0423. Reconstruct Original Digits From English

0423. Reconstruct Original Digits from English #

题目 #

  • 给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9)。按 升序 返回原始的数字。
  • 例如:
输入:s = "owoztneoer"
输出:"012"

思路 #

模拟 #

代码 #

模拟 #

class Solution {
    public String originalDigits(String s) {
        /**
            zero           ->          'z'
            one                                       ->        'o'
            two            ->          'w'
            three                                     ->        'h'
            four           ->          'u'
            five                                      ->        'f' 
            six            ->          'x'
            seven                                     ->        's'         
            eight          ->          'g'
            nine                                                              ->     'i'
         */
        int[] cnt = new int[26];
        for (int i = 0; i < s.length(); i++) cnt[s.charAt(i)-'a']++;
        String ans = "";

        int[] statistics = new int[10];
        while (cnt['z'-'a'] > 0) { statistics[0]++; cnt['z'-'a']--; cnt['e'-'a']--; cnt['r'-'a']--; cnt['o'-'a']--; }
        while (cnt['w'-'a'] > 0) { statistics[2]++; cnt['t'-'a']--; cnt['w'-'a']--; cnt['o'-'a']--; }
        while (cnt['u'-'a'] > 0) { statistics[4]++; cnt['f'-'a']--; cnt['o'-'a']--; cnt['u'-'a']--; cnt['r'-'a']--; }
        while (cnt['x'-'a'] > 0) { statistics[6]++; cnt['s'-'a']--; cnt['i'-'a']--; cnt['x'-'a']--; }
        while (cnt['g'-'a'] > 0) { statistics[8]++; cnt['e'-'a']--; cnt['i'-'a']--; cnt['g'-'a']--; cnt['h'-'a']--; cnt['t'-'a']--; }
        while (cnt['o'-'a'] > 0) { statistics[1]++; cnt['o'-'a']--; cnt['n'-'a']--; cnt['e'-'a']--; }
        while (cnt['h'-'a'] > 0) { statistics[3]++; cnt['t'-'a']--; cnt['h'-'a']--; cnt['r'-'a']--; cnt['e'-'a']--; cnt['e'-'a']--; }
        while (cnt['f'-'a'] > 0) { statistics[5]++; cnt['f'-'a']--; cnt['i'-'a']--; cnt['v'-'a']--; cnt['e'-'a']--; }
        while (cnt['s'-'a'] > 0) { statistics[7]++; cnt['s'-'a']--; cnt['e'-'a']--; cnt['v'-'a']--; cnt['e'-'a']--; cnt['n'-'a']--; }
        while (cnt['i'-'a'] > 0) { statistics[9]++; cnt['n'-'a']--; cnt['i'-'a']--; cnt['n'-'a']--; cnt['e'-'a']--; }

        for (int i = 0; i < statistics.length; i++) if (statistics[i] > 0) ans += String.valueOf(i).repeat(statistics[i]);
        return ans;
    }
}

致谢 #

宫水三叶