Saturday, October 29, 2016

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"
There is a trick in this problem. For all numbers from 0 - 9, there are certain characters that only exists once in the numbers' english expressions:

z: zero 0
w: two 2
u: four 4
x: six 6
g: eight 8

which means from the occurrence of the above five characters, we can find 5 numbers. Now let's take a look of the rest:

o: zero 0, one 1, two 2, four 4

since we already know number of 0, 2, 4, we can calculate number of 1s by nums[1] = counts['o'] - nums[0] - nums[2] - nums[4], similarly, we can find all following numbers:

h: three 3, eight 8
f: four 4, five 5
s: six 6, seven 7
i: five 5, six 6, eight 8, nine 9

public String originalDigits(String s) {
        int[] count = new int[26];
        for (int i = 0; i < s.length(); i++) {
            count[s.charAt(i) - 'a']++;
        }
        
        int[] nums = new int[10];
        nums[0] = count['z' - 'a'];
        nums[2] = count['w' - 'a'];
        nums[4] = count['u' - 'a'];
        nums[6] = count['x' - 'a'];
        nums[8] = count['g' - 'a'];
        nums[1] = count['o' - 'a'] - nums[0] - nums[2] - nums[4];
        nums[3] = count['h' - 'a'] - nums[8];
        nums[5] = count['f' - 'a'] - nums[4];
        nums[7] = count['s' - 'a'] - nums[6];
        nums[9] = count['i' - 'a'] - nums[5] - nums[6] - nums[8];
        
        String rst = "";
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < nums[i]; j++) {
                rst += i;    
            }
        }
        return rst;
    }


1 comment:


  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete