Given a string S of length N, consisting of lowercase characters containing reordered English representations of digits [0 – 9], the task is to print those digits in ascending order.
Examples:
Input: S = “fviefuro”
Output: 45
Explanation: The given string can be reshuffled to “fourfive”, Therefore, the digits represented by the strings are 4 and 5.Input: S = “owoztneoer”
Output: 012
Explanation: The given string can be reshuffled to get “zeroonetwo”, Therefore, the digits represented by the strings are 0, 1 and 2.
Naive Approach: The simplest approach is to generate all permutations of the given string and for each permutation, check if it is possible to find valid digits represented by the string. If found to be true, then print the set of digits in ascending order.
Time Complexity: O(N * N!)
Auxiliary Space: O(1)
Efficient Approach: The idea is based on the observation that some characters only appear in one number.
In ‘zero’, character ‘z’ is unique.
In ‘two’, character ‘w’ is unique.
In ‘four’, character ‘u’ is unique.
In ‘six’, character ‘x’ is unique.
In ‘eight’, character ‘g’ is unique.
In ‘three’, character ‘h’ is unique since word “eight” having character ‘h’ has already been considered.
In ‘one’, character ‘o’ is unique since words having character ‘o’ have already been considered.
In ‘five’, character f’ is unique since word “four” having character ‘f’ has already been considered.
In ‘seven’, character ‘v’ is unique.
In ‘nine’, character ‘i’ is unique since words having character ‘i’ have already been considered.
Follow the steps below to solve the problem:
- Initialize an empty string, ans to store the required result.
- Store the frequency of each character of the string in M.
- Create a mapping of a unique character to its corresponding string.
- Traverse the map, and perform the following steps:
- Store the unique character corresponding to the digit in a variable x.
- Get the occurrence of x in M, and store it in a variable freq.
- Append the corresponding digit, freq number of times to ans.
- Traverse the word corresponding to x and decrement the frequency of its characters by freq in M.
- Print the string, ans as the result.
Below is the implementation of the above approach:
Python3
# Python program to implement the above approachfrom collections import Counter# Function to construct the original set of digits# from the string in ascending orderdef construct_digits(s): # Store the unique characters # corresponding to word and number k = ["z", "w", "u", "x", "g", "h", "o", "f", "v", "i"] l = ["zero", "two", "four", "six", "eight", "three", "one", "five", "seven", "nine"] c = [0, 2, 4, 6, 8, 3, 1, 5, 7, 9] # Store the required result ans = [] # Store the frequency of # each character of S d = Counter(s) # Traverse the unique characters for i in range(len(k)): # Store the count of k[i] in S x = d.get(k[i], 0) # Traverse the corresponding word for j in range(len(l[i])): # Decrement the frequency # of characters by x d[l[i][j]]-= x # Append the digit x times to ans ans.append(str(c[i])*x) # Sort the digits in ascending order ans.sort() return "".join(ans)# Driver Code# Given string, ss = "fviefuro"# Function Callprint(construct_digits(s)) |
C#
// C# program to implement the above approachusing System;using System.Collections.Generic;class GFG{// Function to construct the original set of digits// from the string in ascending orderstatic string construct_digits(string s){ // Store the unique characters // corresponding to word and number char[] k = { 'z', 'w', 'u', 'x', 'g', 'h', 'o', 'f', 'v', 'i' }; string[] l = { "zero", "two", "four", "six", "eight", "three", "one", "five", "seven", "nine" }; int[] c = { 0, 2, 4, 6, 8, 3, 1, 5, 7, 9 }; // Store the required result List<string> ans = new List<string>(); // Store the frequency of // each character of S Dictionary<char, int> d = new Dictionary<char, int>(); for(int i = 0; i < s.Length; i++) { if (!d.ContainsKey(s[i])) d[s[i]] = 0; d[s[i]] += 1; } // Traverse the unique characters for(int i = 0; i < k.Length; i++) { // Store the count of k[i] in S int x = 0; if (d.ContainsKey(k[i])) x = d[k[i]]; // Traverse the corresponding word for(int j = 0; j < l[i].Length; j++) { // Decrement the frequency // of characters by x if (d.ContainsKey(l[i][j])) d[l[i][j]] -= x; } // Append the digit x times to ans ans.Add(((c[i]) * x).ToString()); } // Sort the digits in ascending order ans.Sort(); string str = (String.Join("", ans.ToArray())); return str.Replace("0", "");}// Driver Codepublic static void Main(string[] args){ // Given string, s string s = "fviefuro"; // Function Call Console.WriteLine(construct_digits(s));}}// This code is contributed by ukasp |
45
Time Complexity: O(N*log(N))
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.

