Count unique Strings by replacing Consonant with closest Vowel and vice versa
Last Updated :
29 Jun, 2022
Given a string S consisting of lowercase English alphabets, of length N, the task is to find the total number of distinct strings that can be obtained by performing the given operations on every character of the string S:
- If the character is a consonant, change the character to its closest vowel.
- If the character is a vowel, change the character to its closest consonant.
Note: Do not take distance circularly, so, the distance between the characters a and z is 25 and not 1.
Examples:
Input: S = “face”
Output: 4
Explanation: for ‘f’ closest vowel is ‘e’,
for ‘a’ closest consonant is ‘b’,
for ‘c’ closest vowel is ‘a’ and ‘e’,
for ‘e’ closest consonant is ‘d’ and ‘f’,
So, total distinct strings are 2 * 2 = 4, i.e., “ebad”, “ebaf”, “ebed”, “ebef”.
Input: S = “unique”
Output: 16
Approach: To solve the problem follow the below idea:
- Each vowel except a has two consonants closest to it which are just the previous letter and just the next letter in the English alphabet. So for every vowel there are two choices.
- Each consonant has only 1 vowel closest to it except for ‘c’, ‘g’, ‘l’ and ‘r’ which have two vowels in same distance. So for those there are two choices and all other consonants have only one choice.
Therefore, from the theory of counting, we can say the total number of choices equal to the product of choices for each consonant and each vowel present in the string.
Follow the below steps to solve the problem:
- Initialize the count of distinct strings = 1.
- Run a loop in the string and check,
- If the current character is a vowel then multiply the count by 2 if it is not ‘a’.
- If the current character is a consonant and one among ‘c’, ‘g’, ‘l’ and ‘r’, then multiply the final count by 2.
- The final count after the iteration is over is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findDist(string s)
{
int n = s.size();
int Count = 1;
map< char , int > Mp;
Mp[ 'a' ] = 0;
Mp[ 'e' ] = 4;
Mp[ 'i' ] = 8;
Mp[ 'o' ] = 14;
Mp[ 'u' ] = 20;
for ( int i = 0; i < n; i++) {
if (Mp.find(s[i]) == Mp.end()) {
int x = s[i] - 'a' ;
if (x == 2 || x == 6 || x == 11 || x == 17)
Count *= 2;
}
else {
if (s[i] != 'a' )
Count *= 2;
}
}
return Count;
}
int main()
{
string S = "face" ;
cout << findDist(S) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int findDist(String s)
{
int n = s.length();
int Count = 1 ;
HashMap<Character,Integer> Mp = new HashMap<>();
Mp.put( 'a' , 0 );
Mp.put( 'e' , 4 );
Mp.put( 'i' , 8 );
Mp.put( 'o' , 14 );
Mp.put( 'u' , 20 );
for ( int i = 0 ; i < n; i++) {
if (Mp.containsKey(s.charAt(i)) == false ) {
int x = s.charAt(i) - 'a' ;
if (x == 2 || x == 6 || x == 11 || x == 17 )
Count *= 2 ;
}
else {
if (s.charAt(i) != 'a' )
Count *= 2 ;
}
}
return Count;
}
public static void main(String args[])
{
String S = "face" ;
System.out.println(findDist(S));
}
}
|
Python3
def findDist(s) :
n = len (s);
Count = 1 ;
Mp = {};
Mp[ 'a' ] = 0 ;
Mp[ 'e' ] = 4 ;
Mp[ 'i' ] = 8 ;
Mp[ 'o' ] = 14 ;
Mp[ 'u' ] = 20 ;
for i in range (n) :
if s[i] in Mp :
x = ord (s[i]) - ord ( 'a' );
if (x = = 2 or x = = 6 or x = = 11 or x = = 17 ) :
Count * = 2 ;
else :
if (s[i] ! = 'a' ) :
Count * = 2 ;
return Count;
if __name__ = = "__main__" :
S = "face" ;
print (findDist(S));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int findDist( string s)
{
int n = s.Length;
int Count = 1;
Dictionary< char ,
int > Mp = new Dictionary< char ,
int >();
Mp[ 'a' ] = 0;
Mp[ 'e' ] = 4;
Mp[ 'i' ] = 8;
Mp[ 'o' ] = 14;
Mp[ 'u' ] = 20;
for ( int i = 0; i < n; i++) {
if (Mp.ContainsKey(s[i])) {
int x = s[i] - 'a' ;
if (x == 2 || x == 6 || x == 11 || x == 17)
Count *= 2;
}
else {
if (s[i] != 'a' )
Count *= 2;
}
}
return Count;
}
public static void Main()
{
string S = "face" ;
Console.WriteLine(findDist(S));
}
}
|
Javascript
<script>
function findDist(s)
{
let n = s.length;
let Count = 1;
let Mp= new Map();
Mp.set( 'a' ,0)
Mp.set( 'e' ,4)
Mp.set( 'i' ,8)
Mp.set( 'o' ,14)
Mp.set( 'u' ,20)
for (let i = 0; i < n; i++) {
if (!Mp.has(s[i])) {
let x = s[i] - 'a' ;
if (x == 2 || x == 6 || x == 11 || x == 17)
Count *= 2;
}
else {
if (s[i] != 'a' )
Count *= 2;
}
}
return Count;
}
let S = "face" ;
document.write(findDist(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...