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