Sunday, June 5, 2022
HomeWordPress DevelopmentDepend of attainable Strings by changing consonants with nearest vowel

Depend of attainable Strings by changing consonants with nearest vowel


Enhance Article

Save Article

Like Article

Given a string str consisting of N letters, the duty is to seek out the overall variety of strings that may be generated by changing every consonant with the vowel closest to it within the English alphabet.

Examples:

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

Enter: str = “geeks”
Output: 2

 

Method: The issue may be solved primarily based on the next statement:

There are whole 21 consonant during which ‘c’, ‘g’, ‘l’ and ‘r’ are consonant which is closest to 2 vowels. 
So solely these consonants have 2 selections and the reamining have one selections every.
Subsequently, the overall variety of attainable strings = the product of the variety of selections for every consonant.

Observe the steps talked about beneath to implement the statement:

  • Initialize a variable (say res = 1) to retailer the variety of attainable strings.
  • Iterate by way of the string from i = 0 to N:
    • If the character is likely one of the 4 particular consonants talked about above then they’ve two selections. So multiply 2 with res.
    • In any other case, multiply 1 with the worth of res.
  • The ultimate worth of res is the required reply.

Under is the implementation of the above method:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int uniqueString(string str)

{

    lengthy lengthy int res = 1;

    for (int i = 0; i < str.size(); i++) {

        if (str[i] == 'c' || str[i] == 'g'

            || str[i] == 'l' || str[i] == 'r') {

            res = res * 2;

        }

    }

  

    

    

    return res;

}

  

int predominant()

{

    string str = "code";

  

    

    cout << (uniqueString(str));

    return 0;

}

Java

  

import java.io.*;

  

class GFG {

    

    public static int beautyString(String str)

    {

        char alpha[]

            = str.toCharArray();

        

        int res = 1, depend = 0;

        

        for (int i = 0; i < str.size(); i++) {

            if (alpha[i] == 'c' || alpha[i] == 'g'

                || alpha[i] == 'l' || alpha[i] == 'r') {

                depend++;

                res = res * 2;

            }

        }

        return res;

    }

    

    public static void predominant(String[] args)

    {

        String str = "code";

        

        System.out.println(beautyString(str));

    }

}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments