-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoCharacters.java
47 lines (45 loc) · 1.31 KB
/
twoCharacters.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
package project;
import java.util.Hashtable;
import java.util.Set;
public class twoCharacters {
public static void main(String[] args) {
String s = "beabeefeab";
StringBuffer sb = new StringBuffer(s);
StringBuffer output = new StringBuffer();
for(int i=1; i<sb.length();i++){
if(sb.charAt(i-1)==sb.charAt(i)){
for(int j=0;j<sb.length();j++){
if(sb.charAt(i) != sb.charAt(j)) {
// If it is not the same, append it.
output.append(sb.charAt(j));
}
}
i=0;
sb = output;
}
}
Hashtable<Character,Integer> tb = new Hashtable<Character,Integer>();
for(int i=0;i<output.length();i++) {
if(tb.containsKey(output.charAt(i))){
tb.put(output.charAt(i), tb.get(output.charAt(i))+1);
}else {
tb.put(output.charAt(i), 1);
}
}
sb=output;
StringBuffer op = new StringBuffer();
Set<Character> keys = tb.keySet();
Character[] array = keys.toArray(new Character[0]);
for(Character K:array) {
if(tb.get(K) == 1) {
for(int i=0; i<sb.length();i++) {
if(sb.charAt(i)!=K) {
op.append(sb.charAt(i));
}
}
sb=op;
}
}
System.out.println(sb);
}
}