Consonants Count in Sliding Window
Coding Problem Keys
Consonants Count in Sliding Window
Problem Statement
The program must accept an integer K and a string S containing only alphabets as the input. The program must print the count of consonants in each sliding window of size K in the string S as the output.
Input Format
The first line contains K.
The second line contains S.
Output Format
The first line contains the count of consonants in each sliding window of size K in the string S.
Example Input/Output 1
Input
3
pineapple
Output
2 1 1 1 2 3 2
Explanation
The sliding window size K = 3.
1st window is pin -> 2 consonants
2nd window is ine -> 1 consonants
3rd window is nea -> 1 consonants
4th window is eap -> 1 consonants
5th window is app -> 2 consonants
6th window is ppl -> 3 consonants
7th window is ple -> 2 consonants
Example Input/Output 2
Input
2
PROGRAMMING
Output
2 1 1 2 1 1 2 1 1 2
Max Execution Time Limit: 10 millisecs
Solution
Programming Language: C Language
#include <stdio.h> #include <stdlib.h> int isConsonant(char ch){ ch=tolower(ch); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') return 0; else return 1; } int main() { int k,count=0; scanf("%d",&k); char str[100001]; scanf("%s",str); for(int index=0;index<k;index++){ if(isConsonant(str[index])){ count++; } } printf("%d ",count); for(int index=1;index<=strlen(str)-k;index++){ if(isConsonant(str[index-1])){ count--; } if(isConsonant(str[index+k-1])){ 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