-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Without_Repeat.cpp
More file actions
34 lines (33 loc) · 904 Bytes
/
Copy pathString_Without_Repeat.cpp
File metadata and controls
34 lines (33 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* leetcode 3
Given a string s, find the length of the longest substring without repeating characters.
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. */
#include<string>
#include<iostream>
#include<unordered_map>
using namespace std;
int main(){
string s="pwwkew";
unordered_map <char, int> map;
int left = 0, right = 0, maxSize = 0;
int strLen = s.size();
while(right != strLen){
if(!map.count(s[right])){
map[s[right++]] = 1;
maxSize++;
}
else{
map.erase(s[left++]);
}
}
cout << map.size();
return 0;
}