forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG_LC3_LongestUniqueSubstring.cpp
142 lines (111 loc) · 3.31 KB
/
GFG_LC3_LongestUniqueSubstring.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
https://leetcode.com/problems/longest-substring-without-repeating-characters/
3. Longest Substring Without Repeating Characters
*/
//GFG
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int longestUniqueSubsttr(string S){
//code
int n = S.length();
int maxSoFar=0;
int left = 0;
vector<int> seen(26);
for(int right=0; right<n; right++)
{
seen[S[right]-'a']++;
while(seen[S[right]-'a']>1)
{
seen[S[left]-'a']--;
left++;
}
maxSoFar = max(maxSoFar, right-left+1);
}
return maxSoFar;
}//end
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
Solution ob;
cout<<ob.longestUniqueSubsttr(str)<<endl;
}
return 0;
} // } Driver Code Ends
//LEECTODE
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.length();
if(n==1) return 1; // String with one character is always unique, return 1
int max_so_far =0;
/***** NAIVE APPROACH - Iterating all substrings ****/
// for(int start=0; start<n; start++)
// {
// for(int end=start; end<n; end++)
// {
// vector<bool> visited(256,false);
// bool isUniqueSubstring = true;
// //check if the substring contains all unique characters
// for(int k=start;k<=end;k++)
// {
// if(visited[s[k]] == true)
// {
// isUniqueSubstring = false;
// break;
// }
// visited[s[k]] = true;
// }
// if(isUniqueSubstring)
// {
// max_so_far = max(max_so_far, end-start+1);
// }
// }
// }
// // vector<bool> visited(256,false);
// bitset<256> visited;
// visited.reset();
// int start=0;
// for(int end=0; end<n; end++)
// {
// if(visited[s[end]] == false)
// {
// // visited[s[end]]=true;
// visited.set(s[end]);
// max_so_far = max(max_so_far, end-start+1);
// }
// else
// {
// // visited[s[start]]=false;
// visited.reset(s[start]);
// start++;
// end--;
// }
// }
// return max_so_far;
/*** Sliding window ****/
unordered_map<char,int> seen_freq;
int start=0;
for(int e=0; e<n; e++)
{
seen_freq[s[e]]++; // increase the seen freq of character
while(seen_freq[s[e]] > 1) // end char found is duplcate
{
seen_freq[s[start]]--; // from start decrease the freq of all till we find the duplicate character
start++;
}
max_so_far = max(max_so_far, e-start+1);
}
return max_so_far;
}//end
};