Skip to content
Related Articles

Related Articles

Count unique Strings by replacing Consonant with closest Vowel and vice versa

View Discussion
Improve Article
Save Article
  • 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++




// C++ code to implement the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find distinct
// number of strings
int findDist(string s)
{
 
    // Calculate size of string
    int n = s.size();
 
    // Initialize Count
    int Count = 1;
 
    // Initialize map with vowels and
    // its ASCII value
    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 current character is consonant
        if (Mp.find(s[i]) == Mp.end()) {
            int x = s[i] - 'a';
            if (x == 2 || x == 6 || x == 11 || x == 17)
                Count *= 2;
        }
 
        // Current character is vowel
        else {
            if (s[i] != 'a')
                Count *= 2;
        }
    }
    return Count;
}
 
// Driver Code
int main()
{
    string S = "face";
 
    // Function call
    cout << findDist(S) << endl;
    return 0;
}

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.*;
 
class GFG {
  // Java code to implement the above approach
 
  // Function to find distinct
  // number of strings
  static int findDist(String s)
  {
 
    // Calculate size of string
    int n = s.length();
 
    // Initialize Count
    int Count = 1;
 
    // Initialize map with vowels and
    // its ASCII value
    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 current character is consonant
      if (Mp.containsKey(s.charAt(i)) == false) {
        int x = s.charAt(i) - 'a';
        if (x == 2 || x == 6 || x == 11 || x == 17)
          Count *= 2;
      }
 
      // Current character is vowel
      else {
        if (s.charAt(i) != 'a')
          Count *= 2;
      }
    }
    return Count;
  }
 
  /* Driver program to test above function*/
  public static void main(String args[])
  {
    String S = "face";
 
    // Function call
    System.out.println(findDist(S));
  }
}
 
// This code is contributed by shinjanpatra.

Python3




# Python3 code to implement the above approach
 
# Function to find distinct
# number of strings
def findDist(s) :
 
    # Calculate size of string
    n = len(s);
 
    # Initialize Count
    Count = 1;
 
    # Initialize map with vowels and
    # its ASCII value
    Mp = {};
    Mp['a'] = 0;
    Mp['e'] = 4;
    Mp['i'] = 8;
    Mp['o'] = 14;
    Mp['u'] = 20;
 
    for i in range(n) :
 
        # If current character is consonant
        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;
         
        # Current character is vowel
        else :
            if (s[i] != 'a') :
                Count *= 2;
 
    return Count;
 
# Driver Code
if __name__ == "__main__" :
 
    S = "face";
 
    # Function call
    print(findDist(S));
     
    # This code is contributed by AnkThon

C#




// C# program for above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find distinct
  // number of strings
  static int findDist(string s)
  {
 
    // Calculate size of string
    int n = s.Length;
 
    // Initialize Count
    int Count = 1;
 
    // Initialize map with vowels and
    // its ASCII value
    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 current character is consonant
      if (Mp.ContainsKey(s[i])) {
        int x = s[i] - 'a';
        if (x == 2 || x == 6 || x == 11 || x == 17)
          Count *= 2;
      }
 
      // Current character is vowel
      else {
        if (s[i] != 'a')
          Count *= 2;
      }
    }
    return Count;
  }
 
  // Driver code
  public static void Main()
  {
    string S = "face";
 
    // Function call
    Console.WriteLine(findDist(S));
  }
}
 
// This code is contributed by code_hunt.

Javascript




<script>
// Javascript code to implement the above approach
 
// Function to find distinct
// number of strings
function findDist(s)
{
 
    // Calculate size of string
    let n = s.length;
 
    // Initialize Count
    let Count = 1;
 
    // Initialize map with vowels and
    // its ASCII value
    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 current character is consonant
        if (!Mp.has(s[i])) {
            let x = s[i] - 'a';
            if (x == 2 || x == 6 || x == 11 || x == 17)
                Count *= 2;
        }
 
        // Current character is vowel
        else {
            if (s[i] != 'a')
                Count *= 2;
        }
    }
    return Count;
}
 
// Driver Code
 
    let S = "face";
 
    // Function call
    document.write(findDist(S));
   
  // This code is contributed by satwik4409.
    </script>

Output

4

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!