Repository navigation
Expand file tree
/
Copy pathContiguousArray.cpp
More file actions
268 lines (218 loc) · 6.19 KB
/
Copy pathContiguousArray.cpp
File metadata and controls
268 lines (218 loc) · 6.19 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
class Solution
{
public:
int findMaxLength(vector<int>& nums)
{
int n = nums.size();
vector<int> arr(n);
// Convert 0 → -1, keep 1 as it is
for (int i = 0; i < n; i++)
{
arr[i] = (nums[i] == 0) ? -1 : 1;
}
unordered_map<int, int> firstSeen; // prefixSum → earliest index
int prefixSum = 0, maxLen = 0;
for (int i = 0; i < n; i++)
{
prefixSum += arr[i];
if (prefixSum == 0)
{
maxLen = max(maxLen, i + 1); // subarray [0...i] has sum 0
}
else if (firstSeen.count(prefixSum))
{
int len = i - firstSeen[prefixSum];
maxLen = max(maxLen, len);
}
else
{
firstSeen[prefixSum] = i; // store first occurrence only
}
}
return maxLen;
}
};
/*
APPROACH:
---------
- First transform nums: set 0 → -1, 1 → +1.
- Goal: find the longest subarray with sum == 0 (i.e., equal 1s and 0s).
- Use prefix sum + hashmap to track earliest index where each sum occurred.
- If the same prefix sum is seen again, subarray in between has sum 0.
- Handle full-prefix-zero case (i.e., prefixSum == 0) explicitly instead of presetting the map.
TIME: O(n)
SPACE: O(n)
*/
// Similar Question :
// Longest sub-array with equal number of alphabets and numeric characters
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int longestBalancedSubarray(const string& s)
{
int prefixSum = 0, maxLen = 0;
unordered_map<int, int> seen; // prefixSum → earliest index
for (int i = 0; i < s.length(); i++)
{
// Convert alpha to +1 and digit to -1
if (isalpha(s[i]))
{
prefixSum += 1;
}
else if (isdigit(s[i]))
{
prefixSum -= 1;
}
if (prefixSum == 0)
{
maxLen = max(maxLen, i + 1); // full prefix is balanced
}
else if (seen.count(prefixSum))
{
maxLen = max(maxLen, i - seen[prefixSum]); // subarray with sum = 0
}
else
{
seen[prefixSum] = i; // store first occurrence
}
}
return maxLen;
}
int main()
{
string s = "a1b2c3x5m";
cout << "Longest subarray length with equal alphabets and digits: "
<< longestBalancedSubarray(s) << endl;
return 0;
}
/*
APPROACH:
---------
- Treat alphabets as +1, digits as -1.
- Problem reduces to finding longest subarray with sum = 0.
- Use prefix sum + hashmap to track first occurrence of each sum.
- If same sum seen again, subarray in between is balanced.
- Handle full-prefix case separately when prefixSum becomes 0.
- No need to pre-fill seen[0] = -1; we handle it via (prefixSum == 0).
TIME: O(n)
SPACE: O(n)
*/
// Similar Question :
// Longest Subarray with Equal Number of 'a' and 'b'"
#include <iostream>
#include <unordered_map>
using namespace std;
int longestEqualAB(const string& s)
{
int prefixSum = 0, maxLen = 0;
unordered_map<int, int> firstSeen; // prefixSum → first index
for (int i = 0; i < s.length(); i++)
{
// Convert 'a' to +1 and 'b' to -1
if (s[i] == 'a')
prefixSum += 1;
else if (s[i] == 'b')
prefixSum -= 1;
if (prefixSum == 0)
{
maxLen = max(maxLen, i + 1); // entire prefix is balanced
}
else if (firstSeen.count(prefixSum))
{
maxLen = max(maxLen, i - firstSeen[prefixSum]);
}
else
{
firstSeen[prefixSum] = i; // store first occurrence
}
}
return maxLen;
}
int main()
{
string s = "aabbaababbbba";
cout << "Longest subarray with equal 'a' and 'b': "
<< longestEqualAB(s) << endl;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPO : Longest Substring with equal A , B and C
#include <iostream>
#include <unordered_map>
using namespace std;
int longestEqualABC(const string& s)
{
unordered_map<string, int> seen; // key = "diffB-diffC", value = index
int a = 0, b = 0, c = 0;
int maxLen = 0;
// Difference of counts
int diffBA = 0, diffCA = 0;
// base case: diff(0,0) seen at index -1
seen["0#0"] = -1;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'a') a++;
if (s[i] == 'b') b++;
if (s[i] == 'c') c++;
diffBA = b - a;
diffCA = c - a;
string key = to_string(diffBA) + "#" + to_string(diffCA);
if (seen.count(key))
{
maxLen = max(maxLen, i - seen[key]);
}
else
{
seen[key] = i;
}
}
return maxLen;
}
int main()
{
string s = "abccbabcaacbb";
cout << "Longest subarray with equal a, b, c: "
<< longestEqualABC(s) << endl;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPO : Longest Substring with equal A , B, C and D
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int longestBalancedABCD(const string& s)
{
unordered_map<string, int> seen; // key = diff_ba#diff_ca#diff_da
int a = 0, b = 0, c = 0, d = 0;
int maxLen = 0;
seen["0#0#0"] = -1; // to handle whole prefix match
for (int i = 0; i < s.length(); ++i)
{
if (s[i] == 'a') ++a;
else if (s[i] == 'b') ++b;
else if (s[i] == 'c') ++c;
else if (s[i] == 'd') ++d;
// Differences from 'a'
int diff_ba = b - a;
int diff_ca = c - a;
int diff_da = d - a;
string key = to_string(diff_ba) + "#" + to_string(diff_ca) + "#" + to_string(diff_da);
if (seen.count(key))
{
maxLen = max(maxLen, i - seen[key]);
}
else
{
seen[key] = i;
}
}
return maxLen;
}
int main()
{
string s = "abdcbdacabcd";
cout << "Longest substring with equal number of a, b, c, d: " << longestBalancedABCD(s) << endl;
return 0;
}