Given a string S consisting of solely lowercase English letters of size N, the duty is to search out the depend of substrings such that every substring comprises at the very least 1 vowel and 1 consonant.
Enter: S = “happybirthday”
Clarification: S will be divided as “ha”, “ppybi”, “rthday”
Enter: S = “geeksforgeeks”
Clarification: S will be divided as “ge“, “ek“, “sfo“, “rge”, “eks”
Strategy: To resolve the issue observe the beneath concept:
The concept is to use the grasping method, traverse the string, and each time we encounter 1 vowel and 1 consonant improve the depend by 1 and reset the variety of vowels and consonants to 0.
Observe the steps beneath to unravel 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
- Every time each are true then think about present subsegment is a legitimate substring, improve ans by 1, make haveVowels = false and haveConsonants = false.
Beneath is the implementation of the above method.
Time Complexity: O(N)
Auxiliary House: O(1)