-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-1156.cpp
More file actions
32 lines (28 loc) · 854 Bytes
/
Problem-1156.cpp
File metadata and controls
32 lines (28 loc) · 854 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
//Problem - 1156
// https://leetcode.com/problems/swap-for-longest-repeated-character-substring/
// O(n*n) solution passes all tc
class Solution {
public:
int maxRepOpt1(string text) {
if(text.size() < 2)
return text.size();
int freq[26] = {0};
int ans = 0;
for(auto c : text)
freq[c - 'a']++;
int curr = 1;
int skip = 0;
for(int i = 0; i < text.length()-1; i++) {
skip = 0, curr = 1;
for(int j = i+1; j < text.length(); j++) {
if((text[j] == text[i] || skip == 0) && curr < freq[text[i] - 'a']) {
if(text[j] != text[i])
skip++;
curr++;
} else break;
}
ans = max(ans, curr);
}
return ans;
}
};