Straightforward Java Solution


  • 0
    E
    public class Solution {
        public String originalDigits(String s) {
            int arr[]=new int[26];
            for(char ch:s.toCharArray()){
                arr[ch-'a']++;
            }
            String res="";
            res+=getDigits(arr,"zero",0,'z');
            res+=getDigits(arr,"two",2,'w');
            res+=getDigits(arr,"four",4,'u');
            res+=getDigits(arr,"six",6,'x');
            res+=getDigits(arr,"eight",8,'g');
            res+=getDigits(arr,"one",1,'o');
            res+=getDigits(arr,"three",3,'t');
            res+=getDigits(arr,"five",5,'f');
            res+=getDigits(arr,"seven",7,'s');
            res+=getDigits(arr,"nine",9,'i');
            char ch[]=res.toCharArray();
            Arrays.sort(ch);
            return new String(ch);
        }
        public String getDigits(int arr[], String str, int digit, char search){
            String res="";
            while(arr[search-'a']>0){
                for(int i=0;i<str.length();i++){
                    arr[str.charAt(i)-'a']--;
                }
                res+=digit+"";
            }
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.