Vowel-Restricted Identical Border Substring — Problem Statement & Solution Guide
Problem Description
Given a string s consisting of lowercase English letters and an integer k, find the length of the longest substring that starts and ends with the same character, and contains at most k vowels ('a', 'e', 'i', 'o', 'u'). If no such substring exists, return 0.
Examples
Input
s = 'aba', k = 1
Output
2
Explanation: Step-by-step: with input s = 'aba' and k = 1, we find the longest substring that starts and ends with the same character and contains at most k vowels. The substring 'ab' does not meet the criteria because it starts with 'a' and ends with 'b'. However, the substring 'ba' also does not meet the criteria for the same reason. The substring 'aba' meets the criteria because it starts and ends with 'a' and contains 1 vowel, which is within the limit of k = 1. Therefore, the length of the longest substring that meets the criteria is 2, which is the length of 'aa' is not present but 'aba' is present and 'a' is the common character.
Input
s = 'cc', k = 0
Output
2
Explanation: Step-by-step: with input s = 'cc' and k = 0, we find the longest substring that starts and ends with the same character and contains at most k vowels. The substring 'cc' meets the criteria because it starts and ends with 'c' and contains 0 vowels, which is within the limit of k = 0. Therefore, the length of the longest substring that meets the criteria is 2.
Constraints
- 1 <= s.length <= 10^5
- 0 <= k <= s.length
- s consists only of lowercase English letters.
Optimal Approach & Strategy
Precompute the prefix sum of vowels to allow O(1) vowel counting. Group all occurrences of each character by index and use a two-pointer approach within each group to find the widest valid window, achieving O(n) total time.
Brute Force Approach
Iterate through every possible substring by checking all pairs of indices (i, j). For each pair, check if s[i] == s[j] and count the vowels in between, keeping track of the maximum length found.
Verified Code Solutions
function vowelRestrictedIdenticalBorderSubstring(s, k) {
if (s.length === 0) return 0;
let maxLen = 0;
let vowelCount = 0;
let vowelSet = new Set(['a', 'e', 'i', 'o', 'u']);
for (let i = 0; i < s.length; i++) {
if (vowelSet.has(s[i])) vowelCount++;
let j = i;
while (j < s.length && s[j] === s[i]) j++;
let len = j - i;
if (vowelCount <= k) maxLen = Math.max(maxLen, len);
vowelCount = 0;
}
return maxLen;
}class Solution {
public int solution(String s, int k) {
int max_length = 0;
String vowels = "aeiou";
for (int i = 0; i < s.length(); i++) {
for (int j = i + 1; j <= s.length(); j++) {
String substring = s.substring(i, j);
if (substring.charAt(0) == substring.charAt(substring.length() - 1)) {
int vowel_count = 0;
for (char c : substring.toCharArray()) {
if (vowels.indexOf(c) != -1) {
vowel_count++;
}
}
if (vowel_count <= k) {
max_length = Math.max(max_length, substring.length());
}
}
}
}
return max_length;
}
}def solution(s, k):
max_length = 0
vowels = set(['a', 'e', 'i', 'o', 'u'])
for i in range(len(s)):
for j in range(i + 1, len(s) + 1):
substring = s[i:j]
if substring[0] == substring[-1]:
vowel_count = sum(1 for char in substring if char in vowels)
if vowel_count <= k:
max_length = max(max_length, len(substring))
return max_lengthfunction vowelRestrictedIdenticalBorderSubstring(s, k) {
if (s.length === 0) return 0;
let maxLen = 0;
let vowelCount = 0;
let vowelSet = new Set(['a', 'e', 'i', 'o', 'u']);
for (let i = 0; i < s.length; i++) {
if (vowelSet.has(s[i])) vowelCount++;
let j = i;
while (j < s.length && s[j] === s[i]) j++;
let len = j - i;
if (vowelCount <= k) maxLen = Math.max(maxLen, len);
vowelCount = 0;
}
return maxLen;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.