Related Articles

# Digits whose alphabetic representations are jumbled in a given string

• Difficulty Level : Hard
• Last Updated : 01 Feb, 2021

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 by 1.
• If s[i] = ‘w’, increase count by 1.
• If s[i] = ‘g’, increase count by 1.
• If s[i] = ‘x’, increase count by 1.
• If s[i] = ‘v’, increase count by 1.
• If s[i] = ‘o’, increase count by 1.
• If s[i] = ‘s’, increase count by 1.
• If s[i] = ‘f’, increase count by 1.
• If s[i] = ‘h’, increase count by 1.
• If s[i] = ‘i’, increase count by 1.
• Now, update the elements of the vector as shown below:
• count = count – count.
• count = count – count.
• count = count – count.
• count = count – (count + count + count).
• count = count – count.
• count = count – (count + count + count).
• 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++

 `// C++ program for the above approach``#include ``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++;``        ``if` `(s[i] == ``'w'``)``            ``arr++;``        ``if` `(s[i] == ``'g'``)``            ``arr++;``        ``if` `(s[i] == ``'x'``)``            ``arr++;``        ``if` `(s[i] == ``'v'``)``            ``arr++;``        ``if` `(s[i] == ``'o'``)``            ``arr++;``        ``if` `(s[i] == ``'s'``)``            ``arr++;``        ``if` `(s[i] == ``'f'``)``            ``arr++;``        ``if` `(s[i] == ``'h'``)``            ``arr++;``        ``if` `(s[i] == ``'i'``)``            ``arr++;``    ``}` `    ``// Update the elements of the vector``    ``arr -= arr;``    ``arr -= arr;``    ``arr -= arr;``    ``arr -= (arr + arr + arr);``    ``arr -= arr;``    ``arr -= (arr + arr + arr);` `    ``// 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

 `// 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

 `# 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#

 `// 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``;` `    ``// 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++;``      ``if` `(s[i] == ``'w'``)``        ``arr++;``      ``if` `(s[i] == ``'g'``)``        ``arr++;``      ``if` `(s[i] == ``'x'``)``        ``arr++;``      ``if` `(s[i] == ``'v'``)``        ``arr++;``      ``if` `(s[i] == ``'o'``)``        ``arr++;``      ``if` `(s[i] == ``'s'``)``        ``arr++;``      ``if` `(s[i] == ``'f'``)``        ``arr++;``      ``if` `(s[i] == ``'h'``)``        ``arr++;``      ``if` `(s[i] == ``'i'``)``        ``arr++;``    ``}` `    ``// Update the elements of the vector``    ``arr -= arr;``    ``arr -= arr;``    ``arr -= arr;``    ``arr -= (arr + arr + arr);``    ``arr -= arr;``    ``arr -= (arr + arr + arr);` `    ``// 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`
Output:
`124`

Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up