2017-03-05 00:26:42 -06:00
|
|
|
// Author: Rodney Shaghoulian
|
2018-07-27 16:45:56 -07:00
|
|
|
// Github: github.com/RodneyShag
|
|
|
|
|
// HackerRank: hackerrank.com/RodneyShag
|
2017-03-05 00:26:42 -06:00
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
2017-04-05 18:42:35 -05:00
|
|
|
// Time Complexity: O(n)
|
2017-03-05 00:26:42 -06:00
|
|
|
// Space Complexity: O(n)
|
|
|
|
|
|
2017-04-05 18:42:35 -05:00
|
|
|
// Can alternatively use an ArrayDeque instead of a Stack. Just make sure to iterate
|
|
|
|
|
// through it properly since iteration order is opposite that of a Stack.
|
|
|
|
|
|
2017-03-05 00:26:42 -06:00
|
|
|
public class Solution {
|
2017-09-11 09:32:17 -07:00
|
|
|
public static void main(String[] args) {
|
2017-04-05 18:42:35 -05:00
|
|
|
/* Save input */
|
2017-03-05 00:26:42 -06:00
|
|
|
Scanner scan = new Scanner(System.in);
|
|
|
|
|
String str = scan.next();
|
|
|
|
|
scan.close();
|
|
|
|
|
|
|
|
|
|
/* Iterate through String, creating final result in a Stack */
|
|
|
|
|
Stack<Character> stack = new Stack<>();
|
|
|
|
|
for (int i = 0; i < str.length(); i++) {
|
|
|
|
|
Character ch = str.charAt(i);
|
|
|
|
|
if (!stack.isEmpty() && ch == stack.peek()) {
|
|
|
|
|
stack.pop(); // since String has 2 adjacent equal characters
|
|
|
|
|
} else {
|
|
|
|
|
stack.push(ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print final result */
|
|
|
|
|
if (stack.isEmpty()) {
|
|
|
|
|
System.out.println("Empty String");
|
|
|
|
|
} else {
|
|
|
|
|
for (char ch : stack) {
|
|
|
|
|
System.out.print(ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|