Tuesday, August 23, 2022
HomeWordPress DevelopmentMaximize rely of Substrings containing not less than 1 vowel and 1...

Maximize rely of Substrings containing not less than 1 vowel and 1 consonant


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given a string S consisting of solely lowercase English letters of size N, the duty is to seek out the rely of substrings such that every substring incorporates not less than 1 vowel and 1 consonant.

Examples:

Enter: S = “happybirthday”
Output: 3
Clarification: S could be divided as “ha”, “ppybi”, “rthday”

Enter: S = “geeksforgeeks”
Output 5
Clarification: S could be divided as ge“, “ek“, “sfo“, “rge”, “eks”

 

Method: To resolve the issue observe the beneath thought:

The concept is to use the grasping method, traverse the string, and each time we encounter 1 vowel and 1 consonant enhance the rely by 1 and reset the variety of vowels and consonants to 0.

Observe the steps beneath to resolve the issue:

  • Initialize variable ans = 0, haveVowels = false (present string have vowels or not), haveConsonants = false (present string have consonants or not)
  • Journey over the string by every character
    • If present character is vowels make the haveVowels = true else haveConsonants = true
    • Each time each are true then contemplate present subsegment is a sound substring, enhance ans by 1, make haveVowels = false and haveConsonants = false.

Beneath is the implementation of the above method.

Python3

  

def strengthOfString(S):

    ans = 0

    N = len(S)

  

    

    

    haveVowels = False

    haveConsonants = False

  

    

    for s in S:

  

        

        

        if s in "aeiou":

            haveVowels = True

        else:

            haveConsonants = True

  

        

        

        if haveVowels and haveConsonants:

            ans += 1

            haveVowels = False

            haveConsonants = False

  

    

    return ans

  

if __name__ == "__main__":

    S = "happybirthday"

  

    

    ans = strengthOfString(S)

    print(ans)

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