-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPositionsLargeGroups2.java
69 lines (61 loc) · 1.85 KB
/
PositionsLargeGroups2.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
68
69
// leetcode
// 2021.04.08
// wrong answer
import java.util.ArrayList;
import java.util.List;
// 예외 케이스 발생, 다시 해볼것!
public class PositionsLargeGroups2 {
// public static void main(String[] args) {
// System.out.println(largeGroupPositions("abc").toString());
// }
public static List<List<Integer>> largeGroupPositions(String s) {
List<List<Integer>> solution = new ArrayList<>();
List<Integer> position = new ArrayList<>();
StringBuffer str = new StringBuffer(s);
int cnt = 1, max = 0, idx = 0;
char tmp = str.charAt(0);
for (int i = 1; i < str.length(); i++) {
if (tmp == str.charAt(i)) {
cnt++;
if (i == str.length() - 1)
if (cnt > max) {
max = cnt;
idx = i;
}
} else {
if (cnt > max) {
max = cnt;
idx = i;
}
cnt = 1;
tmp = str.charAt(i);
}
}
if (max == 1)
return solution;
else {
position.add(idx - max);
position.add(idx - 1);
solution.add(position);
}
return solution;
}
}
// best solution_USE Two Pointer
/*
class Solution {
public List<List<Integer>> largeGroupPositions(String S) {
List<List<Integer>> ans = new ArrayList();
int i = 0, N = S.length(); // i is the start of each group
for (int j = 0; j < N; ++j) {
if (j == N-1 || S.charAt(j) != S.charAt(j+1)) {
// Here, [i, j] represents a group.
if (j-i+1 >= 3)
ans.add(Arrays.asList(new Integer[]{i, j}));
i = j + 1;
}
}
return ans;
}
}
*/