-
Notifications
You must be signed in to change notification settings - Fork 0
/
J03027_RutGonXauKyTu.java
38 lines (38 loc) · 1.14 KB
/
J03027_RutGonXauKyTu.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
import java.io.*;
import java.math.*;
import java.util.*;
public class J03027_RutGonXauKyTu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
char []c = s.toCharArray();
int n = s.length();
Stack<Character>st = new Stack<>();
int i = 0;
while(i<n-1){
int j = 1;//j dem so ki tu giong nhau lien tiep tinh tu vi tri i
while(i<n-1&&c[i]==c[i+1])
{
++j;
++i;
}
//dieu kien de them c[i] vao stack
if(j%2==1 && (st.empty()||c[i]!=st.peek()))
st.push(c[i]);
//dieu kien de pop c[i] khoi stack
else if(j%2==1&&!st.empty()&&st.peek()==c[i])
st.pop();
i++;
}
if(i==n-1 && (st.empty()||c[i]!=st.peek())){
st.push(c[i]);
}
else if(i==n-1&&!st.empty()&&st.peek()==c[i]){
st.pop();
}
if(st.empty()) System.out.println("Empty String");
else
for(int j = 0;j<st.size();j++)
System.out.print(st.elementAt(j));
}
}