Skip to content

Commit edb55fe

Browse files
authored
Add files via upload
1 parent 734b65b commit edb55fe

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
3. Longest Substring Without Repeating Characters(Medium)
2+
3+
Given a string s, find the length of the longest
4+
substring
5+
without repeating characters.
6+
7+
8+
9+
Example 1:
10+
11+
Input: s = "abcabcbb"
12+
Output: 3
13+
Explanation: The answer is "abc", with the length of 3.
14+
Example 2:
15+
16+
Input: s = "bbbbb"
17+
Output: 1
18+
Explanation: The answer is "b", with the length of 1.
19+
Example 3:
20+
21+
Input: s = "pwwkew"
22+
Output: 3
23+
Explanation: The answer is "wke", with the length of 3.
24+
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
25+
26+
27+
Constraints:
28+
29+
0 <= s.length <= 5 * 104
30+
s consists of English letters, digits, symbols and spaces.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
ans = 0
4+
count = collections.Counter()
5+
6+
l = 0
7+
for r, c in enumerate(s):
8+
count[c] += 1
9+
while count[c] > 1:
10+
count[s[l]] -= 1
11+
l += 1
12+
ans = max(ans, r - l + 1)
13+
14+
return ans

0 commit comments

Comments
 (0)