Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Reconstruct Original Digits from English problem solution

YASH PAL, 31 July 2024

In this Leetcode Reconstruct Original Digits from English problem solution, we have given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order.

Leetcode Reconstruct Original Digits from English problem solution

Problem solution in Python.

def originalDigits(self, s: str) -> str:
	
    lst = {'z':0, 'o':0, 'w':0, 't':0, 'u':0, 'f':0, 'x':0, 's':0, 'g':0, 'i':0}
    for i in s:
        if i in lst:
            lst[i] += 1
    lst['o'] -= (lst['z'] + lst['w'] + lst['u'])
    lst['t'] -= (lst['w'] + lst['g']) 
    lst['f'] -= lst['u']
    lst['s'] -= lst['x']
    lst['i'] -= (lst['f'] + lst['x'] + lst['g'])
    
    answer = ""
    for i, x in enumerate(lst.values()):
        answer += (str(i) * x)
    return answer

Problem solution in Java.

class Solution {
    public String originalDigits(String s) {
        
        int[] count = new int[26];
        int[] digits = new int[10];
        StringBuilder str = new StringBuilder ();
        
        for (char c : s.toCharArray ()) {
            ++count[c - 'a'];
        }
        
        digits[0] = count[25];
        digits[2] = count[22];
        digits[4] = count[20];
        digits[6] = count[23];
        digits[8] = count[6];
        digits[1] = count[14] - digits[0] - digits[2] - digits[4];
        digits[3] = count[7] - digits[8];
        digits[5] = count[5] - digits[4];
        digits[7] = count[18] - digits[6];
        digits[9] = count[8] - digits[5] - digits[6] - digits[8];
        
        for (int i = 0; i < 10; i++) {
            while (digits[i]-- != 0) {
                str.append ((char) (i + 48));
            }
        }
        
        return str.toString ();
    }
}

Problem solution in C++.

class Solution {
public:
   string originalDigits(string s) {
       vector<int> op(10, 0);
       vector<int> table(26, 0);
       
       int n = s.length();
       for(int i=0; i<n; i++) table[s[i]-'a']++;

       table['e'-'a']-=table['g'-'a'];
       table['i'-'a']-=table['g'-'a'];
       table['h'-'a']-=table['g'-'a'];
       table['t'-'a']-=table['g'-'a'];
       op[8]+=table['g'-'a'];
       table['g'-'a']=0;

       table['s'-'a']-=table['x'-'a'];
       table['i'-'a']-=table['x'-'a'];
       op[6]+=table['x'-'a'];
       table['x'-'a']=0;

       table['e'-'a']-=table['z'-'a'];
       table['r'-'a']-=table['z'-'a'];
       table['o'-'a']-=table['z'-'a'];
       op[0]+=table['z'-'a'];
       table['z'-'a']=0;

       table['t'-'a']-=table['w'-'a'];
       table['o'-'a']-=table['w'-'a'];
       op[2]+=table['w'-'a'];
       table['w'-'a']=0;

       table['f'-'a']-=table['u'-'a'];
       table['o'-'a']-=table['u'-'a'];
       table['r'-'a']-=table['u'-'a'];
       op[4]+=table['u'-'a'];
       table['u'-'a']=0;

       table['n'-'a']-=table['o'-'a'];
       table['e'-'a']-=table['o'-'a'];
       op[1]+=table['o'-'a'];
       table['o'-'a']=0;

       table['i'-'a']-=table['f'-'a'];
       table['v'-'a']-=table['f'-'a'];
       table['e'-'a']-=table['f'-'a'];
       op[5]+=table['f'-'a'];
       table['f'-'a']=0;

       table['s'-'a']-=table['v'-'a'];
       table['e'-'a']-=2*table['v'-'a'];
       table['n'-'a']-=table['v'-'a'];
       op[7]+=table['v'-'a'];
       table['v'-'a']=0;

       table['n'-'a']-=2*table['i'-'a'];
       table['e'-'a']-=table['i'-'a'];
       op[9]+=table['i'-'a'];
       table['i'-'a']=0;
       
       op[3]+=table['t'-'a'];
       
       string ans="";
       for(int i=0;i<10;i++) {
           ans+=string(op[i], '0'+i);
       }
       return ans;
   }
};

Problem solution in C.

#define SIZE 12000
void function(int *map, int num, char* s){
    int len=strlen(s);
    for(int i=0;i<len;i++){
        map[s[i]-'a']-=num;
    }
}
char* originalDigits(char* s) {
    int *map=(int*)calloc(26,sizeof(int));
    int len=strlen(s);
    for(int i=0;i<len;i++){
        map[s[i]-'a']++;
    }
    char *array[]={
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
    };
    char *ret=(char*)malloc(SIZE*sizeof(char));
    int count=0;
    int *nums=(int*)calloc(10,sizeof(int));
    nums[0]=map['z'-'a'];
    function(map,nums[0],array[0]);
    nums[2]=map['w'-'a'];
    function(map,nums[2],array[2]);
    nums[4]=map['u'-'a'];
    function(map,nums[4],array[4]);
    nums[6]=map['x'-'a'];
    function(map,nums[6],array[6]);
    nums[8]=map['g'-'a'];
    function(map,nums[8],array[8]);
    nums[1]=map['o'-'a'];
    function(map,nums[1],array[1]);
    nums[3]=map['t'-'a'];
    function(map,nums[3],array[3]);
    nums[5]=map['f'-'a'];
    function(map,nums[5],array[5]);
    nums[7]=map['s'-'a'];
    function(map,nums[7],array[7]);
    nums[9]=map['i'-'a'];
    function(map,nums[9],array[9]);
    for(int i=0;i<10;i++){
        for(int j=0;j<nums[i];j++){
             ret[count++]=i+'0';
        }
    }
    ret[count++]='';
    return ret;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes