Skip to content

Commit cef9d93

Browse files
committed
Time: 17 ms (70.53%), Space: 8.4 MB (58.26%) - LeetHub
1 parent 750b30a commit cef9d93

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string str) {
4+
// abcasdfsjfsdllkdf
5+
6+
//l ^
7+
//r ^
8+
9+
// substring - think of sliding window
10+
int n = str.size();
11+
12+
unordered_map<int,int> mp;
13+
int sz = 0;
14+
for(int i = 0, j = 0; i <= j and j < n; j++) {
15+
mp[str[j]]++;
16+
while(mp[str[j]] > 1) {
17+
mp[str[i]]--;
18+
i++;
19+
}
20+
sz = max(sz, j - i + 1);
21+
}
22+
return sz;
23+
}
24+
};

0 commit comments

Comments
 (0)