-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1156.java
More file actions
70 lines (68 loc) · 2.03 KB
/
Copy path1156.java
File metadata and controls
70 lines (68 loc) · 2.03 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
// 1156. Swap For Longest Repeated Character Substring
//
// You are given a string text. You can swap two of the characters in the text.
//
// Return the length of the longest substring with repeated characters.
//
//
//
// Example 1:
//
// Input: text = "ababa"
// Output: 3
// Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa" with length 3.
// Example 2:
//
// Input: text = "aaabaaa"
// Output: 6
// Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa" with length 6.
// Example 3:
//
// Input: text = "aaaaa"
// Output: 5
// Explanation: No need to swap, longest repeated character substring is "aaaaa" with length is 5.
//
//
// Constraints:
//
// 1 <= text.length <= 2 * 104
// text consist of lowercase English characters only.
//
// Runtime 9 ms Beats 65.00% of users with Java
// Memory 40.66 MB Beats 94.00% of users with Java
class Solution {
public int maxRepOpt1(String text) {
int[] occurrences = new int[26];
int max = 0;
for (char c: text.toCharArray()) {
occurrences[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (occurrences[i] > max) {
max = Math.max(max, findMax((char)('a' + i), occurrences[i], text));
}
}
return max;
}
private int findMax(char c, int count, String text) {
int left = 0;
int right = 0;
int len = 0;
boolean isSwapped = false;
while (right < text.length()) {
if (text.charAt(right) != c) {
if (isSwapped == false) {
isSwapped = true;
} else {
while (left <= right && text.charAt(left) == c) {
left++;
}
left++;
}
}
right++;
len = Math.max(len, right - left);
}
return Math.min(len, count);
}
}