-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjava.java
67 lines (56 loc) · 2.24 KB
/
java.java
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
class Solution {
public int minimumLength(String s) {
final Map<Character, List<Integer>> charIndexes = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
final char c = s.charAt(i);
final List<Integer> listOfSameChar = charIndexes.getOrDefault(c, new ArrayList<>());
listOfSameChar.add(i);
charIndexes.put(c, listOfSameChar);
}
// System.out.println(String.format("charIndexes=%s", charIndexes));
int length = s.length();
for (List<Integer> listOfSameChar : charIndexes.values()) {
while (listOfSameChar.size() >= 3) {
listOfSameChar.remove(listOfSameChar.size() - 1);
listOfSameChar.remove(0);
length -= 2;
// System.out.println(String.format(" charIndexes=%s", charIndexes));
}
}
return length;
//
// Time Limit Exceeded!
//
// for (int i = 1; i < s.length(); i++) {
// //System.out.println(s);
// char target = s.charAt(i);
// int leftIdx = -1;
// int rightIdx = -1;
// for (int j = 0; j < i; j++) {
// char current = s.charAt(j);
// //System.out.println(String.format(" LEFT i=%s, target=%s, current=%s", i, target, current));
// if (current == target) {
// leftIdx = j;
// break;
// }
// }
// if (leftIdx == -1) continue;
// for (int j = i + 1; j < s.length(); j++) {
// char current = s.charAt(j);
// //System.out.println(String.format(" RIGHT i=%s, target=%s, current=%s", i, target, current));
// if (current == target) {
// rightIdx = j;
// break;
// }
// }
// if (leftIdx != -1 && rightIdx != -1) {
// //System.out.println(String.format(" RESULT i=%s, leftIdx=%s, rightIdx=%s", i, leftIdx, rightIdx));
// s = s.substring(0, rightIdx) + s.substring(rightIdx + 1);
// s = s.substring(0, leftIdx) + s.substring(leftIdx + 1);
// i = 1;
// }
// }
// System.out.println(s);
// return s.length();
}
}