# Digits whose alphabetic representations are jumbled in a given string

In today's article, we will dive deep into a unique problem related to string manipulation in C++. The problem is "Digits whose alphabetic representations are jumbled in a given string." This problem can serve as an excellent exercise for enhancing your string manipulation and data structure skills in C++.

## Problem Statement

Given a string, the task is to identify the digits whose alphabetic representations are jumbled within the string. For instance, if the input string is "oentow", it has a jumbled representation of the digit two (t, w, o) and one (o, n, e).

## C++ Solution Approach

To solve this problem, we will utilize a hash table or an unordered map in C++ that will store the frequency of the alphabets in the string. Then, we will compare this frequency map with predefined maps for each digit's alphabetic representation. If a digit's representation can be formed from the input string, we will output that digit.

### Example

Here's the C++ code to solve the problem −

#include <iostream>
#include <unordered_map>
#include <vector>

// Array of digit representations
std::string digitRepresentations[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

std::unordered_map<char, int> generateFrequencyMap(std::string str) {
std::unordered_map<char, int> freqMap;
for (char c : str) {
freqMap[c]++;
}
return freqMap;
}

std::vector<int> findJumbledDigits(std::string str) {
std::unordered_map<char, int> strFreqMap = generateFrequencyMap(str);
std::vector<int> digits;

for (int i = 0; i < 10; i++) {
std::unordered_map<char, int> digitFreqMap = generateFrequencyMap(digitRepresentations[i]);
bool canFormDigit = true;

for (auto pair : digitFreqMap) {
if (strFreqMap[pair.first] < pair.second) {
canFormDigit = false;
break;
}
}

if (canFormDigit) {
digits.push_back(i);
}
}

return digits;
}

int main() {
std::string input = "oentow";
std::vector<int> digits = findJumbledDigits(input);

std::cout << "The jumbled digits in the string are: ";
for (int digit : digits) {
std::cout << digit << " ";
}

return 0;
}


### Output

The jumbled digits in the string are: 1 2


## Explanation with a Test Case

Let's consider the string "oentow".

When this string is passed to the findJumbledDigits function, it first generates a frequency map for the string: {'o': 2, 'e': 1, 'n': 1, 't': 1, 'w': 1}.

Then, for each digit from 0 to 9, it generates a frequency map of the alphabetic representation of the digit and checks if this map can be formed from the string's frequency map.

The digit 1's representation "one" has the frequency map {'o': 1, 'n': 1, 'e': 1} and digit 2's representation "two" has the frequency map {'t': 1, 'w': 1, 'o': 1}.

Both of these can be formed from the string's frequency map, so we add these digits to the result.

Finally, it outputs the result: "The jumbled digits in the string are: 1 2".

## Conclusion

This problem shows how we can use frequency mapping to solve complex string manipulation problems in C++. It's a great problem to practice your string and data structure handling skills. Keep practicing such problems to enhance your C++ coding skills.

Updated on: 17-May-2023

148 Views