Given a string S of length N which is the English representation of any number of digits in the range [0 – 9] in jumbled format. The task is to find the digits from this representation.
Note: Print digits in any order
Examples
Input: S = “owoftnuoer”
Output: 124
Explanation: The digits here are jumbled form of one, two and four. Therefore, the required output can be 124 or 421 or 214 etc.Input: S = “zesxrionezoreo”
Output: 0016
Approach: This problem can be solved easily by observing an interesting fact that all even digits have at least one character not present in any other strings, while all odd digits don’t:
Following digits have unique letters:
- zero: Only digit with z
- two: Only digit with w
- four: Only digit with u
- six: Only digit with x
- eight: Only digit with g
For the odd digits, every letter also appears in some other digit. Odd digits in words are {one, three, five, seven, nine}. Follow the steps given below to solve the problem
- Create a vector of strings num that will hold the numbers in English letter from 0 to 9 and a vector of integers, count[] of size 10 and also create an answer string ans to display the numbers.
- Now, traverse the given string from i = 0 to N-1.
- If s[i] = ‘z’, increase count[0] by 1.
- If s[i] = ‘w’, increase count[2] by 1.
- If s[i] = ‘g’, increase count[8] by 1.
- If s[i] = ‘x’, increase count[6] by 1.
- If s[i] = ‘v’, increase count[5] by 1.
- If s[i] = ‘o’, increase count[1] by 1.
- If s[i] = ‘s’, increase count[7] by 1.
- If s[i] = ‘f’, increase count[4] by 1.
- If s[i] = ‘h’, increase count[3] by 1.
- If s[i] = ‘i’, increase count[9] by 1.
- Now, update the elements of the vector as shown below:
- count[7] = count[7] – count[6].
- count[5] = count[5] – count[7].
- count[4] = count[4] – count[5].
- count[1] = count[1] – (count[2] + count[4] + count[0]).
- count[3] = count[3] – count[8].
- count[9] = count[9] – (count[5] + count[6] + count[8]).
- Now, traverse the vector from 0 to 9 and append character (i + ‘0’) to ans, count[i] times.
- Finally, print ans as the required answer.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to convert the jumbled // string into digits string finddigits(string s) { // Strings of digits 0-9
string num[]
= { "zero" , "one" , "two" ,
"three" , "four" , "five" ,
"six" , "seven" , "eight" , "nine" };
// Initialize vector
vector< int > arr(10);
// Initialize answer
string ans = "" ;
// Size of the string
int n = s.size();
// Traverse the string
for ( int i = 0; i < n; i++) {
if (s[i] == 'z' )
arr[0]++;
if (s[i] == 'w' )
arr[2]++;
if (s[i] == 'g' )
arr[8]++;
if (s[i] == 'x' )
arr[6]++;
if (s[i] == 'v' )
arr[5]++;
if (s[i] == 'o' )
arr[1]++;
if (s[i] == 's' )
arr[7]++;
if (s[i] == 'f' )
arr[4]++;
if (s[i] == 'h' )
arr[3]++;
if (s[i] == 'i' )
arr[9]++;
}
// Update the elements of the vector
arr[7] -= arr[6];
arr[5] -= arr[7];
arr[4] -= arr[5];
arr[1] -= (arr[2] + arr[4] + arr[0]);
arr[3] -= arr[8];
arr[9] -= (arr[5] + arr[6] + arr[8]);
// Print the digits into their
// original format
for ( int i = 0; i < 10; i++) {
for ( int j = 0; j < arr[i]; j++) {
ans += ( char )(i + '0' );
}
}
// Return answer
return ans;
} // Driver Code int main()
{ string s = "owoftnuoer" ;
cout << finddigits(s) << endl;
} |
// Java program for the above approach import java.io.*;
class GFG {
// Function to convert the jumbled
// string into digits
static String finddigits(String s)
{
// Strings of digits 0-9
String[] num
= { "zero" , "one" , "two" , "three" , "four" ,
"five" , "six" , "seven" , "eight" , "nine" };
// Initialize vector
int [] arr = new int [ 10 ];
// Initialize answer
String ans = "" ;
// Size of the string
int n = s.length();
// Traverse the string
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == 'z' )
arr[ 0 ]++;
if (s.charAt(i) == 'w' )
arr[ 2 ]++;
if (s.charAt(i) == 'g' )
arr[ 8 ]++;
if (s.charAt(i) == 'x' )
arr[ 6 ]++;
if (s.charAt(i) == 'v' )
arr[ 5 ]++;
if (s.charAt(i) == 'o' )
arr[ 1 ]++;
if (s.charAt(i) == 's' )
arr[ 7 ]++;
if (s.charAt(i) == 'f' )
arr[ 4 ]++;
if (s.charAt(i) == 'h' )
arr[ 3 ]++;
if (s.charAt(i) == 'i' )
arr[ 9 ]++;
}
// Update the elements of the vector
arr[ 7 ] -= arr[ 6 ];
arr[ 5 ] -= arr[ 7 ];
arr[ 4 ] -= arr[ 5 ];
arr[ 1 ] -= (arr[ 2 ] + arr[ 4 ] + arr[ 0 ]);
arr[ 3 ] -= arr[ 8 ];
arr[ 9 ] -= (arr[ 5 ] + arr[ 6 ] + arr[ 8 ]);
// Print the digits into their
// original format
for ( int i = 0 ; i < 10 ; i++) {
for ( int j = 0 ; j < arr[i]; j++) {
ans += ( char )(i + '0' );
}
}
// Return answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
String s = "owoftnuoer" ;
System.out.println(finddigits(s));
}
} // This code is contributed by Dharanendra L V |
# Python3 program for the above approach # Function to convert the jumbled # into digits def finddigits(s):
# Strings of digits 0-9
num = [ "zero" , "one" , "two" , "three" ,
"four" , "five" , "six" , "seven" ,
"eight" , "nine" ]
# Initialize vector
arr = [ 0 ] * ( 10 )
# Initialize answer
ans = ""
# Size of the string
n = len (s)
# Traverse the string
for i in range (n):
if (s[i] = = 'z' ):
arr[ 0 ] + = 1
if (s[i] = = 'w' ):
arr[ 2 ] + = 1
if (s[i] = = 'g' ):
arr[ 8 ] + = 1
if (s[i] = = 'x' ):
arr[ 6 ] + = 1
if (s[i] = = 'v' ):
arr[ 5 ] + = 1
if (s[i] = = 'o' ):
arr[ 1 ] + = 1
if (s[i] = = 's' ):
arr[ 7 ] + = 1
if (s[i] = = 'f' ):
arr[ 4 ] + = 1
if (s[i] = = 'h' ):
arr[ 3 ] + = 1
if (s[i] = = 'i' ):
arr[ 9 ] + = 1
# Update the elements of the vector
arr[ 7 ] - = arr[ 6 ]
arr[ 5 ] - = arr[ 7 ]
arr[ 4 ] - = arr[ 5 ]
arr[ 1 ] - = (arr[ 2 ] + arr[ 4 ] + arr[ 0 ])
arr[ 3 ] - = arr[ 8 ]
arr[ 9 ] - = (arr[ 5 ] + arr[ 6 ] + arr[ 8 ])
# Print the digits into their
# original format
for i in range ( 10 ):
for j in range (arr[i]):
ans + = chr ((i) + ord ( '0' ))
# Return answer
return ans
# Driver Code if __name__ = = '__main__' :
s = "owoftnuoer"
print (finddigits(s))
# This code is contributed by mohit kumar 29 |
// C# program for the above approach using System;
public class GFG
{ // Function to convert the jumbled
// string into digits
static string finddigits( string s)
{
// Initialize vector
int [] arr = new int [10];
// Initialize answer
string ans = "" ;
// Size of the string
int n = s.Length;
// Traverse the string
for ( int i = 0; i < n; i++) {
if (s[i] == 'z' )
arr[0]++;
if (s[i] == 'w' )
arr[2]++;
if (s[i] == 'g' )
arr[8]++;
if (s[i] == 'x' )
arr[6]++;
if (s[i] == 'v' )
arr[5]++;
if (s[i] == 'o' )
arr[1]++;
if (s[i] == 's' )
arr[7]++;
if (s[i] == 'f' )
arr[4]++;
if (s[i] == 'h' )
arr[3]++;
if (s[i] == 'i' )
arr[9]++;
}
// Update the elements of the vector
arr[7] -= arr[6];
arr[5] -= arr[7];
arr[4] -= arr[5];
arr[1] -= (arr[2] + arr[4] + arr[0]);
arr[3] -= arr[8];
arr[9] -= (arr[5] + arr[6] + arr[8]);
// Print the digits into their
// original format
for ( int i = 0; i < 10; i++) {
for ( int j = 0; j < arr[i]; j++) {
ans += ( char )(i + '0' );
}
}
// Return answer
return ans;
}
// Driver Code
static public void Main()
{
string s = "owoftnuoer" ;
Console.WriteLine(finddigits(s));
}
} // This code is contributed by Dharanendra L V |
<script> // JavaScript program for the above approach // Function to convert the jumbled // string into digits function finddigits(s)
{ // Strings of digits 0-9
let num = [ "zero" , "one" , "two" ,
"three" , "four" , "five" ,
"six" , "seven" , "eight" , "nine" ];
// Initialize vector
let arr = new Array(10).fill(0);
// Initialize answer
let ans = "" ;
// Size of the string
let n = s.length;
// Traverse the string
for (let i = 0; i < n; i++) {
if (s[i] == 'z' )
arr[0]++;
if (s[i] == 'w' )
arr[2]++;
if (s[i] == 'g' )
arr[8]++;
if (s[i] == 'x' )
arr[6]++;
if (s[i] == 'v' )
arr[5]++;
if (s[i] == 'o' )
arr[1]++;
if (s[i] == 's' )
arr[7]++;
if (s[i] == 'f' )
arr[4]++;
if (s[i] == 'h' )
arr[3]++;
if (s[i] == 'i' )
arr[9]++;
}
// Update the elements of the vector
arr[7] -= arr[6];
arr[5] -= arr[7];
arr[4] -= arr[5];
arr[1] -= (arr[2] + arr[4] + arr[0]);
arr[3] -= arr[8];
arr[9] -= (arr[5] + arr[6] + arr[8]);
// Print the digits into their
// original format
for (let i = 0; i < 10; i++) {
for (let j = 0; j < arr[i]; j++) {
ans += i;
}
}
// Return answer
return ans;
} // Driver Code let s = "owoftnuoer" ;
document.write(finddigits(s), "</br>" );
// this code is contributed by shinjanpatra. </script> |
124
Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(N)