Given a string S consisting of lowercase English alphabets, of size N, the duty is to search out the whole variety of distinct strings that may be obtained by performing the given operations on each 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.
Word: Don’t take distance circularly, so, the gap between the characters a and z is 25 and never 1.
Examples:
Enter: S = “face”
Output: 4
Rationalization: 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, complete distinct strings are 2 * 2 = 4, i.e., “ebad”, “ebaf”, “ebed”, “ebef”.Enter: S = “distinctive”
Output: 16
Strategy: To resolve the issue observe the beneath concept:
- Every vowel besides a has two consonants closest to it that are simply the earlier letter and simply the following letter within the English alphabet. So for each vowel there are two selections.
- Every consonant has just one vowel closest to it aside from ‘c’, ‘g’, ‘l’ and ‘r’ which have two vowels in identical distance. So for these there are two selections and all different consonants have just one selection.
Due to this fact, from the idea of counting, we will say the whole variety of selections equal to the product of selections for every consonant and every vowel current within the string.
Comply with the beneath steps to resolve the issue:
- Initialize the depend of distinct strings = 1.
- Run a loop within the string and examine,
- If the present character is a vowel then multiply the depend by 2 if it isn’t ‘a’.
- If the present character is a consonant and one amongst ‘c’, ‘g’, ‘l’ and ‘r’, then multiply the ultimate depend by 2.
- The ultimate depend after the iteration is over is the required reply.
Under is the implementation of the above strategy:
C++
|
Time Complexity: O(N)
Auxiliary House: O(1)