-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperReducedString.java
More file actions
57 lines (49 loc) · 1.42 KB
/
SuperReducedString.java
File metadata and controls
57 lines (49 loc) · 1.42 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
52
53
54
55
56
57
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static String super_reduced_string(String s){
if (s.length() == 0) {
return "Empty String";
}
StringBuffer sb = new StringBuffer();
char[] chars = s.toCharArray();
int i = 0;
while (i < chars.length) {
// find same char
char c = chars[i];
int count = 0;
for (int j = i; j < chars.length; j++) {
//System.out.println(c);
if (c == chars[j]) {
count += 1;
} else {
break;
}
}
//System.out.println(count);
if ((count % 2) != 0) {
sb.append(c);
}
i += count;
}
if (sb.length() == 0) {
return "Empty String";
} else {
String result = sb.toString();
if (result.equals(s)) {
return result;
} else {
return super_reduced_string(result);
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String result = super_reduced_string(s);
System.out.println(result);
}
}