-
Notifications
You must be signed in to change notification settings - Fork 523
/
Longest Substring Without Repeating Characters.cpp
60 lines (47 loc) · 1.63 KB
/
Longest Substring Without Repeating Characters.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Leetcode problem
Author : Ashish Kumar Sahoo
3. Longest Substring Without Repeating Characters
Intiuation :
Intuition is strightforward and simple, we track the frequencey and as we know we can't use string to track longest substring without repeating characters, as poping a char from front of string is not in O(1) which could be optimized by deque approch.
Approch :
1) At first we initialize a unordered_map to track the frequncy and then.
2) We initialize deque for pushing the characters and if temp > res we update our res deque to temp as we need longest substring here.
3) And while loop is used to reduce the frequency from front doing i++ and removing charecters from temp deque as we no longer need them.
4) return res.size() as we only need size not string.
Time Complexity :
O(N)
Space Complexity :
O(N)
I hope this helps to understand.
Thank you!!
*/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.size()==1) return 1;
unordered_map<char,int>m;
int n = s.length();
deque<char>temp;
deque<char>res;
int i,j;
for(i=0,j=0;i<n && j<n;) {
m[s[j]]++;
if(m[s[j]]>1) {
if(temp.size()>res.size()) {
res = temp;
}
while(m[s[j]]>1) {
temp.pop_front();
m[s[i]]--;
i++;
}
}
temp.push_back(s[j]);
j++;
}
if(temp.size()>res.size()) {
res = temp;
}
return res.size();
}
};