Open In App

Length of the smallest sub-string consisting of maximum distinct characters

Last Updated : 30 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string of length N, find the length of the smallest sub-string consisting of maximum distinct characters. Note : Our output can have same character. 

Examples: 

Input : "AABBBCBB"
Output : 5

Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"

Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"

 Method 1 (Brute Force)

We can consider all sub-strings one by one and check for each sub-string both conditions together 

  1. sub-string’s distinct characters is equal to maximum distinct characters 
  2. sub-string’s length should be minimum. 

Implementation:

C++





Java





Python3





C#





PHP





Javascript





Output

 The length of the smallest substring consisting of maximum distinct characters : 5

Time Complexity : O(n3)
Auxiliary Space: O(n) 

Method 2 (Efficient) 

  1. Count all distinct characters in given string.
  2. Maintain a window of characters. Whenever the window contains all characters of given string, we shrink the window from left side to remove extra characters and then compare its length with smallest window found so far.

Implementation:

C++





Java





Python3





C#




/* C# program to find the length of the smallest
substring consisting of maximum distinct characters */
using System;
using System.Collections.Generic;
 
class GFG
{
    static int MAX_CHARS = 256;
 
    // Find maximum distinct characters in any string
    static int max_distinct_char(String str, int n)
    {
          // Initialize all character's count with 0
        int[] count = new int[MAX_CHARS];
        int max_distinct = 0;
        // Increase the count of max_distinct if a character
        // is found to have a frequency of 1
        for (int i = 0; i < n; i++) {
            count[str[i]]++;
            if (count[str[i]] == 1)
                max_distinct++;
        }
        return max_distinct;
    }
 
    static int smallestSubstr_maxDistictChar(String str)
    {
        int n = str.Length;
        // number of unique characters
        int unique = max_distinct_char(str, n);
        // to store the result
          int res = int.MaxValue;
       
        Dictionary<char, int> mp  = new Dictionary<char, int>();
        int j = 0; // starting index of window
       
        for (int i = 0; i < n; i++) {
            // add the current character in window
            if (mp.ContainsKey(str[i]))
                mp[str[i]]++;
            else
                mp.Add(str[i], 1);
             
            // while no. of distinct elements in the map is
            // equal to unique characters and starting
            // element of the window has frequency more than
            // one we keep reducing its frequency and
            // increasing the starting point of the window
            while (mp.Count == unique && mp[str[j]] > 1)
            {
                mp[str[j]]--;
                j++;
            }
            // if size of map is equal to unique elements
            // update the result
            if (mp.Count == unique)
                res = Math.Min(i - j + 1, res);
        }
        return res;
       
    }
 
    /* Driver program to test above function */
    static public void Main(String[] args)
    {
        // Input String
        String str = "AABBBCBB";
 
        int len = smallestSubstr_maxDistictChar(str);
        Console.WriteLine(" The length of the smallest substring"
                + " consisting of maximum distinct "
                + "characters : "+len);
    }
}
 
// This code contributed by Abhijeet Kumar(abhijeet19403)


Javascript





Output

 The length of the smallest substring consisting of maximum distinct characters : 5

Time Complexity: O(n), As we doing linear operations on string.
Auxiliary Space: O(n), As constant extra space is used. The size of set and map can only go upto a maximum size of 256 which is a constant thus the extra space used is also constant.

Please refer Smallest window that contains all characters of string itself  more details.
Asked In : DailyHunt 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads