Monday, June 27, 2022
HomeSoftware 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 seek out the overall 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.

Be aware: Don’t take distance circularly, so, the space between the characters a and z is 25 and never 1.

Examples:

Enter: S = “face”
Output: 4
Clarification: 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

 

Method: To resolve the issue observe the under concept:

  • Every vowel besides a has two consonants closest to it that are simply the earlier letter and simply the subsequent letter within the English alphabet. So for each vowel there are two decisions.
  • 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 decisions and all different consonants have just one selection.

Due to this fact, from the principle of counting, we will say the overall variety of decisions equal to the product of decisions for every consonant and every vowel current within the string.

Observe the under steps to resolve the issue:

  • Initialize the rely of distinct strings = 1.
  • Run a loop within the string and verify,
    • If the present character is a vowel then multiply the rely 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 last rely by 2.
  • The ultimate rely after the iteration is over is the required reply.

Beneath is the implementation of the above strategy:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int findDist(string s)

{

  

    

    int n = s.dimension();

  

    

    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())

  

        

        else {

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

                Rely *= 2;

        }

    }

    return Rely;

}

  

int important()

{

    string S = "face";

  

    

    cout << findDist(S) << endl;

    return 0;

}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments