
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Number of Vowels in a Substring of Given Length in C++
Suppose we have a string s and an integer k. We have to find the maximum number of vowel letters in any substring of s with length k.
So, if the input is like s = "abciiidef", k = 3, then the output will be 3
To solve this, we will follow these steps −
cnt := 0
Define one set m
for each vowel v, do
insert v into m
ret := 0
for initialize i := 0, when i < k, update (increase i by 1), do −
cnt := cnt + (1 when s[i] is in m, otherwise 0)
ret := maximum of ret and cnt
n := size of s
for initialize i := k, when i < n, update (increase i by 1), do −
if s[i - k] is member of m, then −
(decrease cnt by 1)
cnt := cnt + (1 when s[i] is in m, otherwise 0)
ret := maximum of ret and cnt
return ret
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int maxVowels(string s, int k) { int cnt = 0; set<char> m; for (auto it : { 'a', 'e', 'i', 'o', 'u' }) m.insert(it); int ret = 0; for (int i = 0; i < k; i++) { cnt += m.count(s[i]) ? 1 : 0; } ret = max(ret, cnt); int n = s.size(); for (int i = k; i < n; i++) { if (m.count(s[i - k])) { cnt--; } cnt += m.count(s[i]) ? 1 : 0; ret = max(ret, cnt); } return ret; } }; main(){ Solution ob; cout << (ob.maxVowels("abciiidef",3)); }
Input
"abciiidef",3
Output
3
- Related Articles
- Maximum Number of Occurrences of a Substring in C++
- Count the number of vowels occurring in all the substrings of given string in C++
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Maximum number of removals of given subsequence from a string in C++
- Java program to count the number of vowels in a given sentence
- C++ Path Length having Maximum Number of Bends
- Find the Longest Substring Containing Vowels in Even Counts in C++
- Python program to count number of vowels using set in a given string
- Count number of Distinct Substring in a String in C++
- Program to find longest substring of all vowels in order in Python
- Maximum number of Unique integers in Sub- Array of given sizes in C++
- Count the pairs of vowels in the given string in C++
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Find maximum number that can be formed using digits of a given number in C++

Advertisements