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're the programs that solves the problem −

#include <stdio.h>
#include <string.h>

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

// Function to generate frequency map of characters in a string
void generateFrequencyMap(char* str, int freqMap[]) {
   int len = strlen(str);
   for (int i = 0; i < len; i++) {
      freqMap[str[i] - 'a']++;
   }
}

int main() {
   char input[] = "oentow";
   int strFreqMap[26] = {0};  // Frequency map for characters in the input string
   generateFrequencyMap(input, strFreqMap);
    
   printf("The jumbled digits in the string are: ");
   for (int i = 0; i < 10; i++) {
      int digitFreqMap[26] = {0};  // Frequency map for characters in the digit representation
      generateFrequencyMap(digitRepresentations[i], digitFreqMap);
        
      int canFormDigit = 1;
      for (int j = 0; j < 26; j++) {
         if (strFreqMap[j] < digitFreqMap[j]) {
            canFormDigit = 0;
            break;
         }
      }
        
      if (canFormDigit) {
         printf("%d ", i);
      }
   }

   return 0;
}

Output

The jumbled digits in the string are: 1 2 
#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 
import java.util.HashMap;
import java.util.Map;

public class JumbledDigitsProgram {
   // Map of digit representations
   static Map<Integer, String> digitRepresentations = new HashMap<>();
   static {
      digitRepresentations.put(0, "zero");
      digitRepresentations.put(1, "one");
      digitRepresentations.put(2, "two");
      digitRepresentations.put(3, "three");
      digitRepresentations.put(4, "four");
      digitRepresentations.put(5, "five");
      digitRepresentations.put(6, "six");
      digitRepresentations.put(7, "seven");
      digitRepresentations.put(8, "eight");
      digitRepresentations.put(9, "nine");
   }

   // Function to generate frequency map of characters in a string
   static int[] generateFrequencyMap(String str) {
      int[] freqMap = new int[26];
      for (char c : str.toCharArray()) {
         freqMap[c - 'a']++;
      }
      return freqMap;
   }

   public static void main(String[] args) {
      String input = "oentow";
      int[] strFreqMap = generateFrequencyMap(input);

      System.out.print("The jumbled digits in the string are: ");
      for (int i = 0; i < 10; i++) {
         int[] digitFreqMap = generateFrequencyMap(digitRepresentations.get(i));
         boolean canFormDigit = true;
            
         for (int j = 0; j < 26; j++) {
            if (strFreqMap[j] < digitFreqMap[j]) {
               canFormDigit = false;
               break;
            }
         }

         if (canFormDigit) {
            System.out.print(i + " ");
         }
      }
   }
}

Output

The jumbled digits in the string are: 1 2 
# Dictionary of digit representations
digit_representations = {
   0: "zero", 1: "one", 2: "two", 3: "three", 4: "four",
   5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine"
}

# Function to generate frequency map of characters in a string
def generate_frequency_map(s):
   freq_map = [0] * 26
   for c in s:
      freq_map[ord(c) - ord('a')] += 1
   return freq_map

if __name__ == "__main__":
   input_str = "oentow"
   str_freq_map = generate_frequency_map(input_str)
    
   print("The jumbled digits in the string are:", end=" ")
   for i in range(10):
      digit_freq_map = generate_frequency_map(digit_representations[i])
      can_form_digit = all(str_freq_map[ord(c) - ord('a')] >= digit_freq_map[ord(c) - ord('a')] for c in digit_representations[i])
        
      if can_form_digit:
         print(i, end=" ")

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: 16-Oct-2023

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements