Skip to content

Commit 3686e70

Browse files
committed
큰 그룹 찾기
1 parent 6da5e06 commit 3686e70

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

PositionsLargeGroups2.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// leetcode
2+
// 2021.04.08
3+
// wrong answer
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
9+
// 예외 케이스 발생, 다시 해볼것!
10+
public class PositionsLargeGroups2 {
11+
12+
// public static void main(String[] args) {
13+
// System.out.println(largeGroupPositions("abc").toString());
14+
// }
15+
16+
public static List<List<Integer>> largeGroupPositions(String s) {
17+
List<List<Integer>> solution = new ArrayList<>();
18+
List<Integer> position = new ArrayList<>();
19+
StringBuffer str = new StringBuffer(s);
20+
int cnt = 1, max = 0, idx = 0;
21+
char tmp = str.charAt(0);
22+
23+
for (int i = 1; i < str.length(); i++) {
24+
if (tmp == str.charAt(i)) {
25+
cnt++;
26+
if (i == str.length() - 1)
27+
if (cnt > max) {
28+
max = cnt;
29+
idx = i;
30+
}
31+
} else {
32+
if (cnt > max) {
33+
max = cnt;
34+
idx = i;
35+
}
36+
cnt = 1;
37+
tmp = str.charAt(i);
38+
}
39+
}
40+
if (max == 1)
41+
return solution;
42+
else {
43+
position.add(idx - max);
44+
position.add(idx - 1);
45+
solution.add(position);
46+
}
47+
return solution;
48+
}
49+
}
50+
51+
// best solution_USE Two Pointer
52+
/*
53+
class Solution {
54+
public List<List<Integer>> largeGroupPositions(String S) {
55+
List<List<Integer>> ans = new ArrayList();
56+
int i = 0, N = S.length(); // i is the start of each group
57+
for (int j = 0; j < N; ++j) {
58+
if (j == N-1 || S.charAt(j) != S.charAt(j+1)) {
59+
// Here, [i, j] represents a group.
60+
if (j-i+1 >= 3)
61+
ans.add(Arrays.asList(new Integer[]{i, j}));
62+
i = j + 1;
63+
}
64+
}
65+
66+
return ans;
67+
}
68+
}
69+
*/

0 commit comments

Comments
 (0)