We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a405b1f commit 2ee21efCopy full SHA for 2ee21ef
random-tasks/lengthOfLongestSubstring.cpp
@@ -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