423. Reconstruct Original Digits from English

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"

思路:首先按顺序,找出依次能辨认出来的数字,程序如下:

class Solution {
    public String originalDigits(String s) {
        String[] str = new String[]{"zero", "two", "four", "five", "seven", "six", "one", "three", "nine", "eight"};
        char[] c = new char[]{'z', 'w', 'u', 'f', 'v', 's', 'o', 'r', 'n', 't'};
        char[] num = new char[]{'0', '2', '4', '5', '7', '6', '1', '3', '9', '8'};
        int[] ch = new int[26];
        int len = s.length();
        for (int i = 0; i < len; ++ i){
            ch[s.charAt(i) - 'a'] ++;
        }
        StringBuilder sb = new StringBuilder();
        for (int i= 0; i < str.length; ++ i){  
            if (ch[c[i] - 'a'] != 0){
                sb.append(num[i]);
                int l = str[i].length();
                for (int j = 0; j < l; ++ j){
                    ch[str[i].charAt(j) - 'a'] --;
                }
                i --;
            }
            
        }
        String tmp = sb.toString();
        char[] a = tmp.toCharArray();
        Arrays.sort(a);
        return new String(a);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值