forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperReducedString.java
More file actions
51 lines (40 loc) ยท 1.31 KB
/
SuperReducedString.java
File metadata and controls
51 lines (40 loc) ยท 1.31 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
package hackerrank;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SuperReducedString {
//์์๋ฆฌ์ ๋์ผํ ์ํ๋ฒณ์ด ์๋ค๋ฉด ์ ๊ฑฐํ์ฌ ๋จ๋ ๋ฌธ์๋ฅผ ๋ฆฌํดํ๋ ๋ฌธ์ .
static String superReducedString(String s) {
//List์ s ๋๋ ์ ๋ฃ๊ธฐ
List<String> l = new ArrayList<>();
/* for๋ฌธ๊ณผ addAll() ๋์ผ
for(String a : s.split("")) l.add(a);
*/
Collections.addAll(l, s.split(""));
int i = 0;
while(i != l.size()){
//๋์ด์ ๊ฐ์ ์ํ๋ฒณ์ด ์์๋ ๋ฐ๋ณต๋ฌธ ์ข
๋ฃ
if((i+1) == l.size()) break;
//์์๋ฆฌ ์ํ๋ฒณ์ด ๊ฐ์ผ๋ฉด ๋ ๋ค ์ ๊ฑฐ
if(l.get(i).equals(l.get(i+1))){
l.remove(i);
l.remove(i);
i = 0;
}else{
i++;
}
}
//list๋ฅผ String์ผ๋ก ๋ณ๊ฒฝ
String reduced = "";
for(String e : l){
reduced += e;
}
return reduced.length() == 0 ? "Empty String" : reduced;
}
public static void main(String[] args) {
System.out.println(superReducedString("abba")+", ans: Empty String");
System.out.println(superReducedString("aaabccddd")+", ans: abd");
System.out.println(superReducedString("aa")+", ans: Empty String");
System.out.println(superReducedString("baab")+", ans: Empty String");
}
}