Skip to content

Commit 0c099f2

Browse files
authored
Create 3.longest-substring-without-repeating-characters.py
Solution in py
1 parent 593392a commit 0c099f2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
if(s.size()==0)return 0; // did you handle this?
5+
int n = s.size();
6+
int i(0), j(0);
7+
8+
// s[j] is a char, 'A' is 65, 'a' is 97
9+
vector<int> cnt(300, 0); // ASCII length of 255 will be fine
10+
cnt[s[0]]++; // this can crash if string is empty
11+
12+
int ans=1;
13+
while(j!=n-1){
14+
if(cnt[s[j+1]] == 0) {
15+
j++;
16+
cnt[s[j]] = 1;
17+
ans = max(ans, j-i+1);
18+
}
19+
else {
20+
i++;
21+
cnt[s[i]]--;
22+
}
23+
}
24+
return ans;
25+
}
26+
};

0 commit comments

Comments
 (0)