- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count unique Strings by replacing Consonant with closest vowel and vice versa
In this problem, we will count the number of unique strings we can generate by replacing each vowel with the closest consonant and each consonant with the closest vowel.
We can find the number of choices for each character of the string to replace the current character with other characters. After that, we can multiply the number of choices of each character to get the answer.
Problem Statement
We have given an alpha string. We need to count the total number of different strings we can generate from the given string by performing the below operations on each character of the string
Change the vowel to any closest consonant.
Change consonant to any closest vowel.
Don't consider the circular distance.
Sample Examples
Input – alpha = "abcd" Output – 2
Explanation
We can replace 'a' with only 'b'.
We can replace 'b' with only 'a'.
We can replace 'c' with either 'a' or 'e'.
We can replace 'd' with 'e'.
So, the choice to replace each character of the string is 1, 1, 2, 1. When we multiply all choices, we get 2 as an answer.
Input – alpha = "cglr" Output – 16
Explanation
We have two choices to replace each character of the string with two vowels.
Input – alpha = "pppppp" Output – 1
Explanation
As all characters are the same, and we can replace the 'p' with only 'o', the answer is 1.
Approach 1
In this approach, we will store the position of the vowel in the map to find the closest consonant. For each vowel except 'a', there are two choices to replace the character with a consonant.
Also, the 'c', 'g', 'l', and 'r' consonants have two choices to replace with the closest vowels, and other consonants have only 1 choice. For consonants, we can find the choices to replace each character and multiply each choice to get the answer. Basically, the problem is very similar to finding the total permutations according to the given condition.
Algorithm
Step 1 − Initialize the 'cnt' with 1 to store the answer.
Step 2 − Store all vowels in the 'vowels' map with their 0-based index.
Step 3 − Start traversing the string, and find the characters in the 'vowels' map.
Step 4 − If the character doesn't exist in the vowels map, check whether the character is 'c', 'g', 'l', or 'r'. If yes, multiply the 'cnt' value by 2.
Step 5 − If the character exists in the vowels map, and the character is not 'a', multiply the 'cnt' value by 1.
Step 6 − Return the 'cnt' value.
Example
#include <bits/stdc++.h> using namespace std; int calculateUniqueStrings(string alpha) { int len = alpha.size(); int cnt = 1; // Map for vowels map<char, int> vowels; vowels['a'] = 0; vowels['e'] = 4; vowels['i'] = 8; vowels['o'] = 14; vowels['u'] = 20; for (int p = 0; p < len; p++) { // For consonants if (vowels.find(alpha[p]) == vowels.end()) { int ch = alpha[p] - 'a'; // For c, g, l, and r consonants if (ch == 2 || ch == 6 || ch == 11 || ch == 17) cnt *= 2; } // For vowel else { // Each vowel has two choices except 'a' if (alpha[p] != 'a') cnt *= 2; } } return cnt; } int main() { string alpha = "abgd"; cout << "The total unique strings we can get by replacing the characters is " << calculateUniqueStrings(alpha) << endl; return 0; }
Output
The total unique strings we can get by replacing the characters is 2
Time Complexity − O(N) for replacing each character of the string.
Space Complexity − O(1), as we don't use any extra space.
We learned to find a number of distinct string by replacing each character of the string with the nearest vowel or consonant. Programmers may solve the problem of counting the number of distinct strings we can form by replacing only a single character.