-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestRepeatingCharacterReplacement.java
More file actions
94 lines (85 loc) · 2.82 KB
/
Copy pathLongestRepeatingCharacterReplacement.java
File metadata and controls
94 lines (85 loc) · 2.82 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
import java.util.ArrayList;
import java.util.List;
class Solution {
public int incorrectCharacterReplacement(String s, int k) {
char[] chars = s.toCharArray();
int i = 0;
int j = i;
int max = 0;
int currentMax;
List<Character> list1;
List<Character> list2;
while (i < chars.length && j < chars.length) {
j = i;
currentMax = 0;
list1 = new ArrayList<>();
list2 = new ArrayList<>();
while ((list1.size() <= k || list2.size() <= k) && j < chars.length) {
// co jesli listy sa puste
if (list1.isEmpty()) {
list1.add(chars[j]);
currentMax++;
j++;
if (currentMax > max) {
max = currentMax;
}
continue;
}
else if (list2.isEmpty() && chars[j] != list1.get(0) && list2.size()+1 <= k) {
list2.add(chars[j]);
currentMax++;
j++;
if (currentMax > max) {
max = currentMax;
}
continue;
}
// dodajemy element do list1 jesli tam pasuje
if (chars[j] == list1.get(0) && (list2.size() <= k || list1.size()+1 <= k)) {
list1.add(chars[j]);
currentMax++;
}
else if (list2.isEmpty() && 1 > k) {
break;
}
else if (chars[j] == list2.get(0) && (list2.size()+1 <= k || list1.size() <= k)) {
list2.add(chars[j]);
currentMax++;
if (currentMax > max) {
max = currentMax;
}
}
else {
if (currentMax > max) {
max = currentMax;
}
break;
}
if (currentMax > max) {
max = currentMax;
}
j++;
}
i++;
}
return max;
}
public int characterReplacement(String s, int k) {
int[] charCount = new int[26];
int start = 0;
int end = 0;
int tempmax = 0;
int max = 0;
int length = s.length();
while (end < length) {
tempmax = Math.max(tempmax, ++charCount[s.charAt(end) - 'A']);
while (end - start + 1 - tempmax - k > 0) {
charCount[s.charAt(start) - 'A']--;
start++;
}
max = Math.max(max, end - start + 1);
end++;
}
return max;
}
}