Java implementation LeetCode 423 digital reconstruction from English

423. The digital reconstruction from English

Given a non-empty string containing alphanumeric scramble 0-9 English word representation. Ascending the original digital output.

note:

The input contains only lowercase letters.
Input ensure legitimate and can be converted into original digital, which means that as input "abc" or "zerone" is not allowed.
The length of the input string is less than 50,000.
Example 1:

Enter: "owoztneoer"

Output: "012" (zeroonetwo)
Example 2:

Enter: "fviefuro"

Output: "45" (fourfive)

PS:
I think this is the first description of the problem and leetcode same test cases

b( ̄▽ ̄)d

class Solution {
      public String originalDigits(String s) {
        int[] wordCnt = new int[26];
        for (char c : s.toCharArray()) {
            wordCnt[c - 'a']++;
        }

        int[] numCnt = new int[10];
        numCnt[0] = wordCnt['z' - 'a'];
        numCnt[2] = wordCnt['w' - 'a'];
        numCnt[4] = wordCnt['u' - 'a'];
        numCnt[6] = wordCnt['x' - 'a'];
        numCnt[8] = wordCnt['g' - 'a'];

        numCnt[1] = wordCnt['o' - 'a'] - numCnt[0] - numCnt[2] - numCnt[4];
        numCnt[3] = wordCnt['h' - 'a'] - numCnt[8];
        numCnt[5] = wordCnt['f' - 'a'] - numCnt[4];
        numCnt[7] = wordCnt['s' - 'a'] - numCnt[6];

        numCnt[9] = wordCnt['i' - 'a'] - numCnt[5] - numCnt[6] - numCnt[8];

        int len = 0;
        for (int c : numCnt) {
            len += c;
        }        
        char[] res = new char[len];
        
        len = 0;
        for (int i = 0; i <= 9; i++) {
            for (int j = 0; j < numCnt[i]; j++) {
                res[len++] = (char)('0' + i);
            }
        }

        return new String(res);
    }
}
Released 1537 original articles · won praise 20000 + · views 2.11 million +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104885274