top of page
Search

Reconstruct Original Digits from English

Updated: Mar 29, 2021

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"

Solution:

class Solution {
    public String originalDigits(String s) {
        int[] chars = new int[26];
        for(char c:s.toCharArray())
        {
            chars[c-'a']++;
        }
        
        int[] digits=new int[10];
        digits[0]=chars['z'-'a'];
        digits[6]=chars['x'-'a'];
        digits[4]=chars['u'-'a'];
        digits[2]=chars['w'-'a'];
        digits[8]=chars['g'-'a'];
        digits[5]=chars['f'-'a']-digits[4];
        digits[1]=chars['o'-'a']-digits[2]-digits[4]-digits[0];
        digits[3]=chars['t'-'a']-digits[2]-digits[8];
        digits[7]=chars['s'-'a']-digits[6];
        digits[9]=chars['i'-'a']-digits[5]-digits[6]-digits[8];
        
        StringBuilder res=new StringBuilder();
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<digits[i];j++)
            {
                res.append(i);
            }
        }
        return res.toString();
    }
}

22 views0 comments

Recent Posts

See All

Minimum Deletions to Make Character Frequencies Unique

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

Smallest String With A Given Numeric Value

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.

Commentaires


bottom of page