You are viewing a single comment's thread. Return to all comments →
public static String getLetters(String s){ lettersCount = new int[26]; skippable = new int[26]; for(int j=0; j<s.length(); j++){ int index = s.charAt(j)-97; lettersCount[index]+=1; } for(int i=0; i<skippable.length; i++){ lettersCount[i]/=2; skippable[i] = lettersCount[i]; } buffer = new ArrayList<Integer>(); String finalString = ""; for(int i=s.length()-1; i>=0; i--){ char currChar = s.charAt(i); if(buffer.size()==0) buildBuffer(i); boolean isSmallest = true; for(Integer l:buffer){ if(currChar>l){ isSmallest=false; } } if(isSmallest&&lettersCount[(int)(currChar-97)]>0){ finalString += currChar; lettersCount[(int)(currChar-97)]--; } buffer.remove(0); } return finalString; } public static void buildBuffer(int index){ for(int i=index; i>=0; i--){ if(skippable[s.charAt(i)-97]>=0){ buffer.add((int)s.charAt(i)); if(skippable[s.charAt(i)-97]==0) return; skippable[(int)(s.charAt(i)-97)]--; //System.out.println(skippable[s.charAt(i)-97]); } else return; } return; }
Whats the problem with this code? It works for sometest cases
Seems like cookies are disabled on this browser, please enable them to open this website
Reverse Shuffle Merge
You are viewing a single comment's thread. Return to all comments →
Whats the problem with this code? It works for sometest cases