We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My solution in Java. This is good enough approach because it is short, simple, and we dont have to modify the string during the loop until the end of the process.
publicstaticStringsuperReducedString(Strings){// Write your code here/* Idea: aabccb can turn into bccb then bb then empty - It works as similarly as stack does */Stack<Character>st=newStack<>();for(inti=s.length()-1;i>=0;i--){if(!st.isEmpty()&&st.peek()==s.charAt(i)){st.pop();}else{st.push(s.charAt(i));}}StringBuildersb=newStringBuilder();if(st.isEmpty())return"Empty String";while(!st.isEmpty()){sb.append(st.pop());}returnsb.toString();}
Cookie support is required to access HackerRank
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 →
My solution in Java. This is good enough approach because it is short, simple, and we dont have to modify the string during the loop until the end of the process.