423. Reconstruct Original Digits from English

来源:互联网 发布:兼职网络编辑招聘 编辑:程序博客网 时间:2024/05/18 22:52

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

Note:

  1. Input contains only lowercase English letters.
  2. Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
  3. Input length is less than 50,000.

Example 1:

Input: "owoztneoer"Output: "012"

Example 2:

Input: "fviefuro"Output: "45"




例如 “two",字符w只在”two"中出现,在其他的数字中都不出现,那么w字符的个数就代表2的个数。

这样可以确定的是0、2、4、6、8,其他字符可以根据第一步得到的数字个数结合字符分布情况推出来

public class Solution {    public String originalDigits(String s){int len=s.length();int[] carr=new int[128];for(int i=0;i<len;i++)carr[s.charAt(i)]++;int[] num_count=new int[10];num_count[0]=carr['z'];num_count[2]=carr['w'];num_count[4]=carr['u'];num_count[6]=carr['x'];num_count[8]=carr['g'];num_count[1]=carr['o']-num_count[0]-num_count[2]-num_count[4];num_count[3]=carr['h']-num_count[8];num_count[5]=carr['f']-num_count[4];num_count[7]=carr['s']-num_count[6];num_count[9]=(carr['n']-num_count[1]-num_count[7])/2;StringBuilder sb=new StringBuilder();for(int i=0;i<10;i++)for(int j=0;j<num_count[i];j++)sb.append(i);return sb.toString();}}


0 0