# Reconstruct Original Digits from English in C++

Suppose we have a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. There are some properties −

• 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.
• Input length is less than 50,000.

So if the input is like “fviefuro”, then the output will be 45.

To solve this, we will follow these steps −

• nums := an array that is holding the numbers in English letter from 0 to 9.
• make one array count of size 10
• ans := an empty string. and n := size of the string.
• for i in range 0 to n – 1, do
• if s[i] = ‘z’, then increase count by 1
• if s[i] = ‘w’, then increase count by 1
• if s[i] = ‘g’, then increase count by 1
• if s[i] = ‘x’, then increase count by 1
• if s[i] = ‘v’, then increase count by 1
• if s[i] = ‘o’, then increase count by 1
• if s[i] = ‘s’, then increase count by 1
• if s[i] = ‘f’, then increase count by 1
• if s[i] = ‘h’, then increase count by 1
• if s[i] = ‘i’, then increase count by 1
• count := count – count
• count := count – count
• count := count – count
• count := count – (count + count + count)
• count := count – count
• count := count – (count + count + count)
• for i in range 0 to 9, do
• for j in range 0 to count[i]
• ans := ans + character of (i + ‘0’)
• return ans

## Example(C++)

Let us see the following implementation to get better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string originalDigits(string s) {
string nums[]= {"zero", "one", "two", "three", "four", "five", "six", "seven","eight", "nine"};
vector <int> cnt(10);
string ans = "";
int n = s.size();
for(int i = 0; i < n; i++){
if(s[i] == 'z')cnt++;
if(s[i] == 'w') cnt++;
if(s[i] == 'g')cnt++;
if(s[i] == 'x')cnt++;
if(s[i] == 'v')cnt++;
if(s[i] == 'o')cnt++;
if(s[i] == 's')cnt++;
if(s[i] == 'f')cnt++;
if(s[i] == 'h')cnt++;
if(s[i] == 'i') cnt++;
}
cnt -= cnt;
cnt -= cnt;
cnt -= cnt;
cnt -= (cnt + cnt + cnt);
cnt -= cnt;
cnt -= (cnt + cnt + cnt);
for(int i = 0; i < 10; i++){
for(int j = 0; j < cnt[i]; j++){
ans += (char)(i + '0');
}
}
return ans;
}
};
main(){
Solution ob;
cout << ob.originalDigits("fviefuro");
}

## Input

"fviefuro"

## Output

"45"

Updated on: 28-Apr-2020

254 Views 