Home >Backend Development >C++ >Given a string in which letters represent numbers scrambled

Given a string in which letters represent numbers scrambled

WBOY
WBOYforward
2023-09-11 20:37:02822browse

Given a string in which letters represent numbers scrambled

In today's article, we will delve into a unique issue related to string manipulation in C. This question is "In the given string, the alphabetic expression is scrambled with numbers." This question can serve as a good exercise to improve your string manipulation and data structure skills in C.

Problem Statement

Given a string, the task is to identify numbers in which letter expressions are scrambled. For example, if the input string is "oentow", it has an alphabetical representation of the number 2 (t, w, o) and the number 1 (o, n, e) scrambled.

C Solution Method

To solve this problem, we will use a hash table or unordered map in C to store the frequency of letters in a string. We will then compare this frequency map to a predefined map of the alphabetical representation of each number. If a representation of a number can be formed from the input string, we will output that number.

The Chinese translation of

Example

is:

Example

This is 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

is translated as:

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 number from 0 to 9, it generates a frequency map of the alphabetical representation of the number and checks whether this map can be formed from the frequency map of the string.

The representation "one" of the number 1 has the frequency mapping {'o': 1, 'n': 1, 'e': 1}, while the representation "two" of the number 2 has the frequency mapping {'t' : 1, 'w': 1, 'o': 1}.

These two can be generated by a frequency map of strings, so we add these numbers to the result.

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

in conclusion

This question shows how we can use frequency mapping to solve complex string manipulation problems in C. This is a great question to practice your string and data structure handling skills. Keep practicing questions like this to improve your C coding skills.

The above is the detailed content of Given a string in which letters represent numbers scrambled. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete