Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Maximum k-Substring Vowels Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Maximum k-Substring Vowels

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string s and a number k, find the maximum number of vowels in any substring of size k.

Vowels: ['a', 'e', 'i', 'o', 'u']

Naive Approach

The naive approach is to iterate over all the subarrays in the array, and if it is of length k, we can calculate the number of vowels in it and calculate the maximum out of them.

Analysis

  • Time Complexity: O(n3)
  • Space Complexity: O(1)

Implementation

C++

int maxKSubstringVowels(string s, int k) {
	int n = s.length();
	int result = 0;
	int iterator = 0;
	for(int i = 0; i < n; i++) {
		for(int j = i; j < n; j++) {
			if(j - i + 1 == k) {
				int counter = 0;
				for(int l = i; l <= j; l++) {
					if(s[l] == 'a' || s[l] == 'e' || s[l] == 'i' || s[l] == 'o' || s[l] == 'u') {
						counter++;
					}
				}
				result = max(result, counter);
			}
		}
	}
	return result;
}

Java

class Solution {
	int maxKSubstringVowels (String s, int k) {
		int n = s.length();
		int result = 0;
		int iterator = 0;
		for(int i = 0; i < n; i++) {
			for(int j = i; j < n; j++) {
				if(j - i + 1 == k) {
					int counter = 0;
					for(int l = i; l <= j; l++) {
						if(s.charAt(l) == 'a' || s.charAt(l) == 'e' || s.charAt(l) == 'i' || s.charAt(l) == 'o' || s.charAt(l) == 'u') {
							counter++;
						}
					}
					result = Math.max(result, counter);
				}
			}
		}
		return result;
	}
}

Optimal Approach

We will use the sliding window technique to calculate the number of vowels in subarrays of size k in linear time. Using the sliding window technique we can check if the elements deleted and added into the window is a vowel or not based on which we change our answer. Then we will take the maximum answer over all the subarrays.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++

int isVowel(char c) {
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
		return 1;
	}
	return 0;
}
int maxKSubstringVowels(string s, int k) {
	int n = s.length();
	int count = 0;
	for (int i = 0; i < k; i++) {
		count += isVowel(s[i]);
	}

	int maxCount = count;
	for (int i = k; i < n; i++) {
		count = count - isVowel(s[i - k]) + isVowel(s[i]);
		maxCount = max(maxCount, count);
	}

	return maxCount;
}

Java

class Solution {
	int isVowel(char c) {
		if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
			return 1;
		}
		return 0;
	}

	int maxKSubstringVowels (String s, int k) {
		int n = s.length();
		int count = 0;
		for (int i = 0; i < k; i++) {
			count += isVowel(s.charAt(i));
		}

		int maxCount = count;
		for (int i = k; i < n; i++) {
			count = count - isVowel(s.charAt(i - k)) + isVowel(s.charAt(i));
			maxCount = Math.max(maxCount, count);
		}

		return maxCount;
	}
}
Related Content
Dutch National Flag
k-diff pairs
K-Subarray Sum
k-Substring Vowels
Kth element of two sorted lists
Maximum K-Subarray Sum
Merge Two Sorted Arrays
Remove occurences
Sorted Arrays Intersection
Three Sum
Trapping Rain Water
Two Sum Sorted
Unique Elements in Sorted Array
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet