Maximum Number of Vowels in a Substring of Given Length

Maximum number of vowels in a Substring of given length

In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum Number of Vowel Letters present in any Substring of the String, with the length of each Substring equal to K."

Let us understand this statement with some Sample inputs:

Now, we will discuss different approaches to solve the problem and analyze their complexities.

Approach – 1 Generate All Substrings Using substring() Method

  • Basically, we will iterate throughout the length of the String and generate [all substrings](https://java2blog.com/find-all-substrings-of-string-in-java/ “all substrings”) of the given length K using the substring() method present in String class of java.lang package.
  • We Keep a Variable maxVowels which will be used to track the Maximum Vowels for a substring. So, For each substring, we count the number of Vowels in it and update the maxVowels.
  • To count the number of vowels in a substring, we implement countVowelsInSubstring(), a method which given a substring will return the count of Vowels in it.

Let us look at the code for this approach:

Output:

Time Complexity: Generating a Substring of size K using substring() method takes O(K) time and we do this for N-K characters taking total O( (N-K) * K ) time. Now, for each substring of size K, we count the vowels in it which takes total O( K * K) = O(K2) time, so the Overall [time complexity](https://java2blog.com/introduction-to-complexity-of-algorithm/ “time complexity”) will be : O( N*K -K2 + K2) = O(N * K). This is a naïve solution for this problem.

Approach – 2 Using Sliding Window Method (Linear Time Solution)

The intention behind this approach is to give a Linear Runtime Solution(O(n)) to this problem. For this, we are going to use the idea of the Sliding Window Method. We follow these steps:

  • At first, we will calculate the number of vowels in the first window of size K i.e. from the index 0 to K-1 as countVowels. We have a variable maxVowels that keeps track of the maximum number of vowels in a window.
  • If the First Window has vowels we update the maxVowels. Then we start traversing from index K throughout the length of the String and calculate the vowels in each window. Consider the String "java2blog" the vowels present at different windows of the string is shown below:
  • We use the idea of prefix array, after getting the vowels in 1st window, we start from the index K, if the character at index K is a vowel we increment the countVowels and if the character at the index i - k is a Vowel we decrement countVowels by 1. This step ensures the window slides to the right in every iteration.
  • Finally, we update the maxVowels for each window that we process.

Now, let us have a look at the implementation of above approach in Code:

Output:

Time Complexity: Basically, We slide the window throughout the length of string, N, and traverse each character only once. For each character, we check whether it is a Vowel which is an O(1) operation. So the Time Complexity of this approach is O(N) and gives an optimal solution to the problem.

That’s all about maximum number of vowels in a substring of Given Length, you can try the code with different examples considering all edge cases.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *