-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapForLongestRepeatedCharacterSubstring.cpp
More file actions
50 lines (48 loc) · 1.54 KB
/
swapForLongestRepeatedCharacterSubstring.cpp
File metadata and controls
50 lines (48 loc) · 1.54 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
// Source: https://leetcode.com/problems/swap-for-longest-repeated-character-substring/
// Author: Miao Zhang
// Date: 2021-04-13
class Solution {
public:
int maxRepOpt1(string text) {
int n = text.length();
// Length of repeted substring ends with text[i]
vector<vector<int>> len1(n, vector<int>(26));
// Length of repeated substring starts with text[i]
vector<vector<int>> len2(n, vector<int>(26));
vector<int> counts(26);
int last = -1;
int l = 0;
int res = 0;
for (int i = 0; i < n; ++i) {
int c = text[i] - 'a';
if (c == last) {
++l;
} else {
last = c;
l = 1;
}
len1[i][c] = l;
len2[i - l + 1][c] = l;
++counts[c];
res = max(res, l);
}
for (int i = 1; i < n - 1; ++i) {
int cl = text[i - 1] - 'a';
int cr = text[i + 1] - 'a';
int left = len1[i - 1][cl];
int right = len2[i + 1][cr];
int count = 0;
if (cl != cr) {
// e.g. "c aaa b cccc" => "b aaa ccccc"
count = max(left + (counts[cl] > left ? 1 : 0),
right + (counts[cr] > right ? 1 : 0));
} else {
// e.g. "a c aaa b aaaa" => "b c aaaaaaaa"
count = left + right;
if (counts[cl] > count) ++count;
}
res = max(res, count);
}
return res;
}
};