You are viewing a single comment's thread. Return to all comments →
Easy to read Java with stack
public static String superReducedString(String s) { // Write your code here Stack<Character> stack = new Stack(); char[] chars = s.toCharArray(); for(char c: chars){ if(!stack.isEmpty() && c == stack.peek()){ stack.pop(); }else{ stack.push(c); } } StringBuilder result = new StringBuilder(); while(!stack.isEmpty()){ result.insert(0,stack.pop()); System.out.println(result); } if(result.length()==0){ return "Empty String"; } return result.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 →
Easy to read Java with stack