BackmediumStringsSwiggyMicrosoft

Vowel-Restricted Identical Border Substring Solution

Problem Statement

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.

Example 1
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.

Example 2
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.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Vowel-Restricted Identical Border Substring — Problem Statement & Solution Guide

StringsMediumSliding Window
TimeO(n)
|
SpaceO(1)

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

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(n)
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;
}

Asked in Top Tech Interviews

SwiggyMicrosoft

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.