BackmediumStringsCred

Shortest Substring of Dominant Characters Solution

Problem Statement

Given a string s, find the minimum length of a contiguous substring that contains all occurrences of at least one dominant character. A character is considered 'dominant' if its frequency of occurrence in s is equal to the maximum frequency of any character in s.

Example 1
Input
abba
Output
ab

Explanation: Step-by-step: The dominant character 'a' occurs twice, which is the maximum frequency. The substring 'ab' contains all occurrences of 'a' and it's the shortest possible substring.

Example 2
Input
abcde
Output
a

Explanation: Step-by-step: The dominant character 'a' occurs twice, which is the maximum frequency. The substring 'a' contains all occurrences of 'a' and it's the shortest possible substring.

Constraints

  • 1 <= s.length <= 10^5
  • s consists 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

Shortest Substring of Dominant Characters — Problem Statement & Solution Guide

StringsMediumHash Maps
TimeO(n)
|
SpaceO(n)

Problem Description

Given a string s, find the minimum length of a contiguous substring that contains all occurrences of at least one dominant character. A character is considered 'dominant' if its frequency of occurrence in s is equal to the maximum frequency of any character in s.

Examples

Example 1

Input

abba

Output

ab

Explanation: Step-by-step: The dominant character 'a' occurs twice, which is the maximum frequency. The substring 'ab' contains all occurrences of 'a' and it's the shortest possible substring.

Example 2

Input

abcde

Output

a

Explanation: Step-by-step: The dominant character 'a' occurs twice, which is the maximum frequency. The substring 'a' contains all occurrences of 'a' and it's the shortest possible substring.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters.

Optimal Approach & Strategy

Track the frequency, first occurrence index, and last occurrence index of each character during a single pass. Identify the maximum frequency, then check all characters with this frequency to find the minimum span between their first and last indices.

Brute Force Approach

Generate all possible contiguous substrings, check which ones contain all occurrences of at least one dominant character, and find the minimum length among them. This requires O(N^2) or O(N^3) time complexity.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(s) {
   const maxFrequency = {};
   let maxCount = 0;
   for (let char of s) {
      maxFrequency[char] = (maxFrequency[char] || 0) + 1;
      maxCount = Math.max(maxCount, maxFrequency[char]);
   }
   const dominantChars = Object.keys(maxFrequency).filter(char => maxFrequency[char] === maxCount);
   let minLen = Infinity;
   for (let char of dominantChars) {
      let start = 0;
      let end = 0;
      let count = 0;
      while (end < s.length) {
         if (s[end] === char) {
            count++;
         }
         if (count === maxCount) {
            while (start <= end && count === maxCount) {
               if (s[start] === char) {
                  count--;
               }
               start++;
            }
            minLen = Math.min(minLen, end - start + 1);
         }
         end++;
      }
   }
   return minLen === Infinity ? -1 : minLen;
}

Asked in Top Tech Interviews

Cred

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.