Skip to content

Commit 2ee21ef

Browse files
authored
Create lengthOfLongestSubstring.cpp
1 parent a405b1f commit 2ee21ef

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+
#include<iostream>
2+
#include<string>
3+
#include<map>
4+
5+
int lengthOfLongestSubstring(const std::string& s) {
6+
std::map<char, int> m;
7+
size_t globalMax = 0;
8+
int beg = 0;
9+
10+
for(int i = 0; i < s.size(); ++i) {
11+
if(m[s[i]] > 0) {
12+
if(m.size() > globalMax)
13+
globalMax = m.size();
14+
15+
while(m[s[i]] > 0)
16+
m.erase(s[beg++]);
17+
}
18+
m[s[i]] = 1;
19+
}
20+
21+
return std::max(m.size(), globalMax);
22+
}
23+
24+
int main() {
25+
std::cout << lengthOfLongestSubstring("dvdf");
26+
}

0 commit comments

Comments
 (0)