Given a string which contains only lowercase letters, remove duplicate letters so this every letter appear once and only O nCE. You must make sure your result are the smallest in lexicographical order among all possible results.
Example:
Given"bcabc"
Return"abc"
Given"cbacdcbc"
Return"acdb"
The runtime is O (-* N) = O (n).
Public classSolution {//form a dictionary int[26]//find the smallest value in the current STR or char of CNT 1//Recursive:remove min in front of all char and then remove all of the Charat (min) characters in S; Publicstring Removeduplicateletters (string s) {int[] Count =New int[26]; intMin = 0; for(inti = 0; I < s.length (); i++) {Count[s.charat (i)-' a '] + +; } for(inti = 0; I < s.length (); i++){ if(S.charat (i) < S.charat (min)) min =i; if(--count[s.charat (i)-' a '] = = 0) Break; } returnS.length () ==0? "": S.charat (min) + removeduplicateletters (s.substring (min+1). ReplaceAll ("" +s.charat (Min), "")); }}
Remove Duplicate Letters