Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 356678c

Browse files
committed
added: longest substring without repeating characters
1 parent c233167 commit 356678c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# longest substring without repeating characters | leetcode 03 | https://leetcode.com/problems/longest-substring-without-repeating-characters
2+
# sliding window; remove elements until last occurence of current duplicate
3+
4+
class Solution:
5+
def lengthOfLongestSubstring(self, s: str) -> int:
6+
ptrL = 0
7+
seen = dict()
8+
longest = 0
9+
10+
for ptrR in range(len(s)):
11+
while seen.get(s[ptrR]) is not None:
12+
seen.pop(s[ptrL])
13+
ptrL += 1
14+
seen[s[ptrR]] = True
15+
longest = max(ptrR - ptrL + 1, longest)
16+
17+
return longest
18+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /