Equal Count - Vowels & Consonants
Coding Problem Keys
Equal Count - Vowels & Consonants
Problem Statement
The program must accept a string S (consisting of only vowels and consonants) and an integer N. The program must print the number of substring values of length N which have equal count of vowels and consonants.
Note: The value of N is always even.
Input Format
The first line contains S.
The second line contains N.
Output Format
The first line contains the count of consonants in each sliding window of size K in the string S.
Boundary Condition(s)
1 <= Length of S <= 10⁵
2 <= N <= Length of S
Example Input/Output 1
Input
havenhelljoysorrow
4
Output
5
Explanation
There are 5 substring values of length N = 4, which have equal count of vowels and consonants.
have, aven, enhe, oyso, orro.
Example Input/Output 2
Input
skillrack
2
Output
4
Max Execution Time Limit: 10 millisecs
Solution
Programming Language: C Language
#include <stdio.h> #include <stdlib.h> int isVowel(char ch){ ch=tolower(ch); return ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'; } int main() { char str[100001]; int n,balance=0,count=0; scanf("%s\n%d",str,&n); int len=strlen(str); for(int index=0;index<n;index++){ if(isVowel(str[index])){ balance++; }else{ balance--; } } if(balance==0){ count++; } for(int index=1;index<=len-n;index++){ balance+=(isVowel(str[index-1])?-1:1); balance+=(isVowel(str[index+n-1])?1:-1); if(balance==0){ count++; } } printf("%d ",count); }
// Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments