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++
|
Java
|
Time Complexity: O(N)
Auxiliary Area: O(1)