Super Reduced String

  • + 0 comments

    public static String superReducedString(String s) { char[] arr = s.toCharArray(); //Since String is immutable we can't directly alter the string so //use StringBuilder to store the altered String java.lang.StringBuilder sb = new java.lang.StringBuilder(); for(char c : arr){ if(sb.length()>0 &&(sb.charAt(sb.length()-1)==c)){ sb.deleteCharAt(sb.length()-1); }else{ sb.append(c); } } if(sb.length()==0){ return "Empty String"; } return sb.toString(); }