Open In App

Count of possible Strings by replacing consonants with nearest vowel

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string str consisting of N letters, the task is to find the total number of strings that can be generated by replacing each consonant with the vowel closest to it in the English alphabet.

Examples:

Input: str = “code”
Output: 2
Explanation: Str = “code” has two consonant c and d. 
Closest vowel to d is e and closest to c are a and e.
The possible strings are “aoee” and “eoee

Input: str = “geeks”
Output: 2

 

Approach: The problem can be solved based on the following observation:

There are total 21 consonant in which ‘c’, ‘g’, ‘l’ and ‘r’ are consonant which is closest to two vowels. 
So only these consonants have 2 choices and the remaining have one choices each.
Therefore, the total number of possible strings = the product of the number of choices for each consonant.

Follow the steps mentioned below to implement the observation:

  • Initialize a variable (say res = 1) to store the number of possible strings.
  • Iterate through the string from i = 0 to N:
    • If the character is one of the four special consonants mentioned above then they have two choices. So multiply 2 with res.
    • Otherwise, multiply 1 with the value of res.
  • The final value of res is the required answer.

Below is the implementation of the above approach:

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the total number of
// distinct beautiful strings
int uniqueString(string str)
{
    long long int res = 1;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == 'c' || str[i] == 'g'
            || str[i] == 'l' || str[i] == 'r') {
            res = res * 2;
        }
    }
 
    // Return res as which is
    // total number of possible strings
    return res;
}
 
// Driver code
int main()
{
    string str = "code";
 
    // Function call
    cout << (uniqueString(str));
    return 0;
}


Java




// java code to implement the approach
 
import java.io.*;
 
class GFG {
    // Function to find the total number of distinct beautiful strings
    public static int beautyString(String str)
    {
        char alpha[]
            = str.toCharArray(); // converting string into
        // array
        int res = 1, count = 0;
        // count for character 'c', 'g', 'l' and 'r'
        for (int i = 0; i < str.length(); i++) {
            if (alpha[i] == 'c' || alpha[i] == 'g'
                || alpha[i] == 'l' || alpha[i] == 'r') {
                count++;
                res = res * 2;
            }
        }
        return res;
    }
    // Driver Code
    public static void main(String[] args)
    {
        String str = "code";
        // Function Call
        System.out.println(beautyString(str));
    }
}


Python3




# Python code to implement the approach
 
# Function to find the total number of
# distinct beautiful strings
def uniqueString(s):
    res = 1
    for i in range(len(s)):
        if s[i] == 'c' or s[i] == 'g' or s[i] == 'l' or s[i] == 'r':
            res = res * 2
 
    # Return res as which is
    # total number of possible strings
    return res
 
# Driver code
if __name__ == "__main__":
    s = "code"
     
    # Function call
    print(uniqueString(s))
 
# This code is contributed by Rohit Pradhan


C#




// C# code to implement the above approach
using System;
 
public class GFG {
 
  // Function to find the total number of distinct beautiful strings
  public static int beautyString(string str)
  {
    char []alpha = str.ToCharArray(); // converting string into
    // array
    int res = 1, count = 0;
 
    // count for character 'c', 'g', 'l' and 'r'
    for (int i = 0; i < str.Length; i++) {
      if (alpha[i] == 'c' || alpha[i] == 'g'
          || alpha[i] == 'l' || alpha[i] == 'r') {
        count++;
        res = res * 2;
      }
    }
    return res;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    string str = "code";
 
    // Function Call
    Console.WriteLine(beautyString(str));
  }
}
 
// This code is contributed by AnkThon


Javascript




<script>
// javascript code to implement the above approach
 
// Function to find the total number of
// distinct beautiful strings
function uniqueString(s){
    let res = 1
    for (let i = 0; i < s.length; i++){
        if (s[i] == 'c' | s[i] == 'g' | s[i] == 'l' | s[i] == 'r')
            res = res * 2
}
    // Return res as which is
    // total number of possible strings
    return res
     
}
 
// Driver code
let s = "code"
 
// Function call
document.write(uniqueString(s))
 
// This code is contributed by AnkThon
</script>


Output

2

Time Complexity: O(N)In the above-given approach, there is one loop for iterating over string which takes O(N) time in worst case. Therefore, the time complexity for this approach will be O(N).
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant



Last Updated : 10 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads