Friday, June 17, 2022
HomeWordPress DevelopmentRely distinctive Strings by changing Consonant with closest Vowel and vice versa

Rely distinctive Strings by changing Consonant with closest Vowel and vice versa


View Dialogue

Enhance Article

Save Article

Like Article

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++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int findDist(string s)

{

  

    

    int n = s.measurement();

  

    

    int Rely = 1;

  

    

    

    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 (Mp.discover(s[i]) == Mp.finish()) x == 17)

                Rely *= 2;

        

  

        

        else {

            if (s[i] != 'a')

                Rely *= 2;

        }

    }

    return Rely;

}

  

int most important()

{

    string S = "face";

  

    

    cout << findDist(S) << endl;

    return 0;

}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments