Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Complete the superReducedString function below.
- static String superReducedString(String s) {
- char c = s.charAt(0);
- int i = 0;
- Stack<Character> stack = new Stack<>();
- while (i < s.length()) {
- c = s.charAt(i);
- if (!stack.empty() && stack.peek() == s.charAt(i)) {
- stack.pop();
- } else {
- stack.push(s.charAt(i));
- }
- ++i;
- }
- if (stack.empty()) {
- return "Empty String";
- } else {
- StringBuilder sb = new StringBuilder();
- for (Character ch : stack) {
- sb.append(ch);
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment