Skip to content

Commit a047163

Browse files
committed
Longest Substring Without Repeating Characters solution
1 parent feadd82 commit a047163

File tree

1 file changed

+20
-0
lines changed
  • longest-substring-without-repeating-characters

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
int result = 0;
5+
int start = 0;
6+
unordered_map<char, int> map;
7+
8+
for(int end = 0; end < s.length(); end++){
9+
char ch = s[end];
10+
11+
if(map.count(ch) && map[ch] >= start)
12+
start = map[ch] + 1;
13+
14+
map[ch] = end;
15+
result = max(result, end - start + 1);
16+
}
17+
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)