You are viewing a single comment's thread. Return to all comments →
Java O(n)
public static String superReducedString(String s) { Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (!stack.isEmpty() && stack.peek() == c) { stack.pop(); } else { stack.push(c); } } StringBuilder sb = new StringBuilder(); for (char c : stack) { sb.append(c); } return sb.length() == 0 ? "Empty String" : sb.toString(); }
Seems like cookies are disabled on this browser, please enable them to open this website
Super Reduced String
You are viewing a single comment's thread. Return to all comments →
Java O(n)