Skip to content

Commit d23e153

Browse files
committed
[이진희] Day26
1 parent 5b436c6 commit d23e153

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
3+
1. 아이디어 : 조건에 맞춰 구현
4+
리스트를 사용하여 각 문자열을 압축, 각각의 개수 구함
5+
words 배열을 완전탐색하여 똑같이 계산 후, 늘릴수 있는지, 조건에 맞춰 하나하나 비교
6+
7+
2. 시간복잡도 : O(S+N*M) (N = words.length, M = words 배열의 각 단어의 길이)
8+
9+
3. 자료구조/알고리즘 : 구현
10+
11+
*/
12+
13+
class Solution {
14+
private List<int[]> ori;
15+
16+
public int expressiveWords(String s, String[] words) {
17+
ori = new ArrayList<>();
18+
char memo = s.charAt(0);
19+
int cnt = 1;
20+
int ans = 0;
21+
22+
for(int i=1; i<s.length(); i++) {
23+
char c = s.charAt(i);
24+
if(c != memo) {
25+
ori.add(new int[]{memo, cnt});
26+
memo = c;
27+
cnt = 1;
28+
}
29+
else cnt++;
30+
}
31+
ori.add(new int[]{memo, cnt});
32+
33+
for(String word : words) {
34+
if(checkExpressiveWords(word)) ans++;
35+
}
36+
37+
return ans;
38+
}
39+
40+
private boolean checkExpressiveWords(String s) {
41+
List<int[]> tmp = new ArrayList<>();
42+
char memo = s.charAt(0);
43+
int cnt = 1;
44+
int ans = 0;
45+
46+
for(int i=1; i<s.length(); i++) {
47+
char c = s.charAt(i);
48+
if(c != memo) {
49+
tmp.add(new int[]{memo, cnt});
50+
memo = c;
51+
cnt = 1;
52+
}
53+
else cnt++;
54+
}
55+
tmp.add(new int[]{memo, cnt});
56+
57+
if(tmp.size() != ori.size()) return false;
58+
for(int i=0; i<tmp.size(); i++) {
59+
int[] o = ori.get(i);
60+
int[] t = tmp.get(i);
61+
62+
if(o[0] != t[0]) return false;
63+
if(o[1] < t[1]) return false;
64+
if(o[1] != t[1] && o[1] < 3) return false;
65+
}
66+
67+
return true;
68+
}
69+
}

0 commit comments

Comments
 (0)