Open In App
Related Articles

Minimum length substring with exactly K distinct characters

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a string S and a number K. The task is to find the minimum length substring having exactly K distinct characters. 
Note: The string S consists of only lowercase English alphabets.

Examples: 

Input:  S = "ababcb", K = 3
Output:  abc

Input:  S="efecfefd", K = 4
Output:  cfefd

Naive approach: A simple solution is to consider each substring and check if it contains k distinct characters. If yes then compare the length of this substring with the minimum length substring found earlier. 

  • Generate all the substring
    • Check if the current substring contains exactly k distinct characters
    • Minimize the length of such valid substring and keep it into result.
  • Finally, return the result.

Below is the implementation of the above approach:

C++

// C++ program to find minimum length substring
// having exactly k distinct character.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum length substring
// having exactly k distinct character.
string findMinLenStr(string& s, int k)
{
    int n = s.length();
    string result = "";
    int minn = INT_MAX;
 
    // Generate all the substring
    for (int i = 0; i < n; i++) {
        unordered_map<char, int> unmap;
        string s2 = "";
 
        for (int j = i; j < n; j++) {
            unmap[s[j]]++;
            s2 += s[j];
 
            // Check if current substring contains exactly k
            // distinct characters
            if (unmap.size() == k) {
 
                // Minimise the length of such valid
                // substring and keep it into result.
                if (j - i + 1 < minn) {
                    minn = j - i + 1;
                    result = s2;
                }
                break;
            }
        }
    }
 
    // Finally, return the result.
    return result;
}
 
// Driver code
int main()
{
    string str = "ababcb";
 
    int k = 3;
 
    cout << findMinLenStr(str, k);
 
    return 0;
}

                    

Java

// Java code for the above approach
import java.util.*;
 
class Main {
  public static String findMinLenStr(String s, int k) {
    int n = s.length();
    String result = "";
    int minn = Integer.MAX_VALUE;
 
    // Generate all the substring
    for (int i = 0; i < n; i++) {
      Map<Character, Integer> unmap = new HashMap<>();
      StringBuilder s2 = new StringBuilder();
 
      for (int j = i; j < n; j++) {
        if (unmap.containsKey(s.charAt(j))) {
          unmap.put(s.charAt(j), unmap.get(s.charAt(j)) + 1);
        } else {
          unmap.put(s.charAt(j), 1);
        }
        s2.append(s.charAt(j));
 
        // Check if current substring contains exactly k
        // distinct characters
        if (unmap.size() == k) {
 
          // Minimize the length of such valid
          // substring and keep it into result.
          if (j - i + 1 < minn) {
            minn = j - i + 1;
            result = s2.toString();
          }
          break;
        }
      }
    }
 
    // Finally, return the result.
    return result;
  }
 
  public static void main(String[] args) {
    String str = "ababcb";
    int k = 3;
    System.out.println(findMinLenStr(str, k));
  }
}
 
// This code is contributed by lokeshpotta20.

                    

Python3

# Python program to find minimum length substring
# having exactly k distinct character.
import sys
 
# Function to find minimum length substring
# having exactly k distinct character.
def findMinLenStr( s,  k):
    n = len(s);
    result = "";
    minn = sys.maxsize;
 
    # Generate all the substring
    for i in range(0,n):
        unmap=dict()
        s2 = "";
 
        for j in range(i,n):
            if s[j] in unmap:
                unmap[s[j]]+=1;
            else:
                unmap[s[j]]=1;
            s2 += s[j];
 
            # Check if current substring contains exactly k
            # distinct characters
            if (len(unmap) == k) :
 
                # Minimise the length of such valid
                # substring and keep it into result.
                if (j - i + 1 < minn) :
                    minn = j - i + 1;
                    result = s2;
                 
                break;
             
    # Finally, return the result.
    return result;
 
# Driver code
str = "ababcb";
k = 3;
print(findMinLenStr(str, k));

                    

C#

// C# program to find minimum length substring
// having exactly k distinct character.
 
using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG {
    // Function to find minimum length substring
    // having exactly k distinct character.
    static string findMinLenStr(string s, int k)
    {
        int n = s.Length;
        string result = "";
        int minn = Int32.MaxValue;
     
        // Generate all the substring
        for (int i = 0; i < n; i++) {
            Dictionary<char, int> unmap=new Dictionary<char,int>();
            string s2 = "";
     
            for (int j = i; j < n; j++)
            {
                if(unmap.ContainsKey(s[j]))
                    unmap[s[j]]++;
                else
                    unmap[s[j]]=1;
                s2 += s[j];
     
                // Check if current substring contains exactly k
                // distinct characters
                if (unmap.Count == k) {
     
                    // Minimise the length of such valid
                    // substring and keep it into result.
                    if (j - i + 1 < minn) {
                        minn = j - i + 1;
                        result = s2;
                    }
                    break;
                }
            }
        }
     
        // Finally, return the result.
        return result;
    }
     
    // Driver code
    public static void Main (string[] args)
    {
        string str = "ababcb";
     
        int k = 3;
     
        Console.Write(findMinLenStr(str, k));
    }
}

                    

Javascript

// Javascript program to find minimum length substring
// having exactly k distinct character.
 
// Function to find minimum length substring
// having exactly k distinct character.
function findMinLenStr(s, k)
{
    let n = s.length;
    let result = "";
    let minn = Number.MAX_SAFE_INTEGER;
 
    // Generate all the substring
    for (let i = 0; i < n; i++) {
        let unmap= new Map();
        let s2 = "";
 
        for (let j = i; j < n; j++) {
            unmap[s[j]]++;
            if(unmap.has(s[j]))
                unmap.set(s[j],1);
            else
                unmap.set(s[j],unmap.get(s[j])+1);
            s2 += s[j];
 
            // Check if current substring contains exactly k
            // distinct characters
            if (unmap.size == k) {
 
                // Minimise the length of such valid
                // substring and keep it into result.
                if (j - i + 1 < minn) {
                    minn = j - i + 1;
                    result = s2;
                }
                break;
            }
        }
    }
 
    // Finally, return the result.
    return result;
}
 
// Driver code
    let str = "ababcb";
 
    let k = 3;
 
    document.write(findMinLenStr(str, k));

                    

Output
abc

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Solution: An efficient solution is to use sliding window technique and hashing. The idea is to use two pointers st and end to denote starting and ending point of sliding window. Initially point both to beginning of the string. Move end forward and increment count of corresponding character. If count is one then a new distinct character is found and increment count of number of distinct characters. If count of number of distinct characters is greater than k then move st forward and decrease count of character. If character count is zero then a distinct character is removed and count of distinct elements can be reduced to k this way. If count of distinct elements is k, then remove characters from beginning of sliding window having count greater than 1 by moving st forward. Compare length of current sliding window with minimum length found so far and update if necessary. 
Note that each character is added and removed from sliding window at most once, so each character is traversed twice. Hence the time complexity is linear.

Steps to solve this problem:

1. Declare n=str.length, St=0, end=0.

2. Declare an array cnt of size 26 and initialize it with zero.

3. Declare distele=0, minlen=n,starting=-1 and currlen.

4. While end is smaller than n:

    *Increment cnt[str[end]-‘a’].

    *Check if cnt[str[end]-‘a’] is equal to 1 than distele++.

    *Check distele is greater than k and while St<end && distele>k:

        *Check if cnt[str[St]-‘a’] is equal to 1 than distele–.

    *Check if distele is equal to k and while St<end && cnt[str[St]-‘a’]is greater than 1:

        *Decrement cnt[str[St]-‘a’] and increment St.

        *Update currlen=end-st+1.

        *Check if currlen is smaller than minlen than minlen=currlen and starting=St.

    *Increment end.

5. Return str.substr(starting,minlen).

   

Below is the implementation of above approach: 

C++

// C++ program to find minimum length substring
// having exactly k distinct character.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum length substring
// having exactly k distinct character.
string findMinLenStr(string str, int k)
{
    int n = str.length();
 
    // Starting index of sliding window.
    int st = 0;
 
    // Ending index of sliding window.
    int end = 0;
 
    // To store count of character.
    int cnt[26];
    memset(cnt, 0, sizeof(cnt));
 
    // To store count of distinct
    // character in current sliding
    // window.
    int distEle = 0;
 
    // To store length of current
    // sliding window.
    int currlen;
 
    // To store minimum length.
    int minlen = n;
 
    // To store starting index of minimum
    // length substring.
    int startInd = -1;
 
    while (end < n) {
 
        // Increment count of current character
        // If this count is one then a new
        // distinct character is found in
        // sliding window.
        cnt[str[end] - 'a']++;
        if (cnt[str[end] - 'a'] == 1)
            distEle++;
 
        // If number of distinct characters is
        // is greater than k, then move starting
        // point of sliding window forward,
        // until count is k.
        if (distEle > k) {
            while (st < end && distEle > k) {
                if (cnt[str[st] - 'a'] == 1)
                    distEle--;
                cnt[str[st] - 'a']--;
                st++;
            }
        }
 
        // Remove characters from the beginning of
        // sliding window having count more than 1
        // to minimize length.
        if (distEle == k) {
            while (st < end && cnt[str[st] - 'a'] > 1) {
                cnt[str[st] - 'a']--;
                st++;
            }
 
            // Compare length with minimum length
            // and update if required.
            currlen = end - st + 1;
            if (currlen < minlen) {
                minlen = currlen;
                startInd = st;
            }
        }
 
        end++;
    }
 
    // Return minimum length  substring.
    return str.substr(startInd, minlen);
}
 
// Driver code
int main()
{
    string str = "efecfefd";
 
    int k = 4;
 
    cout << findMinLenStr(str, k);
 
    return 0;
}

                    

Java

// Java program to find minimum length subString
// having exactly k distinct character.
class GFG
{
 
// Function to find minimum length subString
// having exactly k distinct character.
static String findMinLenStr(String str, int k)
{
    int n = str.length();
 
    // Starting index of sliding window.
    int st = 0;
 
    // Ending index of sliding window.
    int end = 0;
 
    // To store count of character.
    int cnt[] = new int[26];
    for(int i = 0; i < 26; i++)cnt[i] = 0;
 
    // To store count of distinct
    // character in current sliding
    // window.
    int distEle = 0;
 
    // To store length of current
    // sliding window.
    int currlen;
 
    // To store minimum length.
    int minlen = n;
 
    // To store starting index of minimum
    // length subString.
    int startInd = -1;
 
    while (end < n)
    {
 
        // Increment count of current character
        // If this count is one then a new
        // distinct character is found in
        // sliding window.
        cnt[str.charAt(end) - 'a']++;
        if (cnt[str.charAt(end) - 'a'] == 1)
            distEle++;
 
        // If number of distinct characters is
        // is greater than k, then move starting
        // point of sliding window forward,
        // until count is k.
        if (distEle > k)
        {
            while (st < end && distEle > k)
            {
                if (cnt[str.charAt(st) - 'a'] == 1)
                    distEle--;
                cnt[str.charAt(st) - 'a']--;
                st++;
            }
        }
 
        // Remove characters from the beginning of
        // sliding window having count more than 1
        // to minimize length.
        if (distEle == k)
        {
            while (st < end && cnt[str.charAt(st) - 'a'] > 1)
            {
                cnt[str.charAt(st) - 'a']--;
                st++;
            }
 
            // Compare length with minimum length
            // and update if required.
            currlen = end - st + 1;
            if (currlen < minlen)
            {
                minlen = currlen;
                startInd = st;
            }
        }
 
        end++;
    }
 
    // Return minimum length subString.
    return str.substring(startInd,startInd + minlen);
}
 
// Driver code
public static void main(String args[])
{
    String str = "efecfefd";
    int k = 4;
    System.out.println(findMinLenStr(str, k));
}
}
 
// This code is contributed by Arnab Kundu

                    

Python 3

# Python 3 program to find minimum length
# substring having exactly k distinct character.
 
# Function to find minimum length substring
# having exactly k distinct character.
def findMinLenStr(str, k):
 
    n = len(str)
 
    # Starting index of sliding window.
    st = 0
 
    # Ending index of sliding window.
    end = 0
 
    # To store count of character.
    cnt = [0] * 26
 
    # To store count of distinct
    # character in current sliding
    # window.
    distEle = 0
 
    # To store length of current
    # sliding window.
    currlen =0
 
    # To store minimum length.
    minlen = n
 
    # To store starting index of minimum
    # length substring.
    startInd = -1
 
    while (end < n):
 
        # Increment count of current character
        # If this count is one then a new
        # distinct character is found in
        # sliding window.
        cnt[ord(str[end]) - ord('a')] += 1
        if (cnt[ord(str[end]) - ord('a')] == 1):
            distEle += 1
 
        # If number of distinct characters is
        # is greater than k, then move starting
        # point of sliding window forward,
        # until count is k.
        if (distEle > k):
            while (st < end and distEle > k):
                if (cnt[ord(str[st]) -
                        ord('a')] == 1):
                    distEle -= 1
                cnt[ord(str[st]) - ord('a')] -= 1
                st += 1
 
        # Remove characters from the beginning of
        # sliding window having count more than 1
        # to minimize length.
        if (distEle == k):
            while (st < end and cnt[ord(str[st]) -
                                    ord('a')] > 1):
                cnt[ord(str[st]) - ord('a')] -= 1
                st += 1
 
            # Compare length with minimum length
            # and update if required.
            currlen = end - st + 1
            if (currlen < minlen):
                minlen = currlen
                startInd = st
 
        end += 1
 
    # Return minimum length substring.
    return str[startInd : startInd + minlen]
 
# Driver code
if __name__ == "__main__":
     
    str = "efecfefd"
 
    k = 4
 
    print(findMinLenStr(str, k))
 
# This code is contributed by Ita_c

                    

C#

// C# program to find minimum length subString
// having exactly k distinct character.
using System;
 
class GFG
{
 
    // Function to find minimum length subString
    // having exactly k distinct character.
    static String findMinLenStr(string str, int k)
    {
        int n = str.Length;
     
        // Starting index of sliding window.
        int st = 0;
     
        // Ending index of sliding window.
        int end = 0;
     
        // To store count of character.
        int []cnt = new int[26];
        for(int i = 0; i < 26; i++)cnt[i] = 0;
     
        // To store count of distinct
        // character in current sliding
        // window.
        int distEle = 0;
     
        // To store length of current
        // sliding window.
        int currlen;
     
        // To store minimum length.
        int minlen = n;
     
        // To store starting index of minimum
        // length subString.
        int startInd = -1;
     
        while (end < n)
        {
     
            // Increment count of current character
            // If this count is one then a new
            // distinct character is found in
            // sliding window.
            cnt[str[end] - 'a']++;
            if (cnt[str[end] - 'a'] == 1)
                distEle++;
     
            // If number of distinct characters is
            // is greater than k, then move starting
            // point of sliding window forward,
            // until count is k.
            if (distEle > k)
            {
                while (st < end && distEle > k)
                {
                    if (cnt[str[st] - 'a'] == 1)
                        distEle--;
                    cnt[str[st] - 'a']--;
                    st++;
                }
            }
     
            // Remove characters from the beginning of
            // sliding window having count more than 1
            // to minimize length.
            if (distEle == k)
            {
                while (st < end && cnt[str[st] - 'a'] > 1)
                {
                    cnt[str[st] - 'a']--;
                    st++;
                }
     
                // Compare length with minimum length
                // and update if required.
                currlen = end - st + 1;
                if (currlen < minlen)
                {
                    minlen = currlen;
                    startInd = st;
                }
            }
     
            end++;
        }
     
        // Return minimum length subString.
        return str.Substring(startInd, minlen);
    }
     
    // Driver code
    public static void Main()
    {
        string str = "efecfefd";
        int k = 4;
        Console.WriteLine(findMinLenStr(str, k));
    }
}
 
// This code is contributed by Ryuga

                    

Javascript

<script>
 
 
// Javascript program to find minimum length substring
// having exactly k distinct character.
 
// Function to find minimum length substring
// having exactly k distinct character.
function findMinLenStr(str, k)
{
    var n = str.length;
 
    // Starting index of sliding window.
    var st = 0;
 
    // Ending index of sliding window.
    var end = 0;
 
    // To store count of character.
    var cnt = Array(26).fill(0);
 
    // To store count of distinct
    // character in current sliding
    // window.
    var distEle = 0;
 
    // To store length of current
    // sliding window.
    var currlen;
 
    // To store minimum length.
    var minlen = n;
 
    // To store starting index of minimum
    // length substring.
    var startInd = -1;
 
    while (end < n) {
 
        // Increment count of current character
        // If this count is one then a new
        // distinct character is found in
        // sliding window.
        cnt[str[end].charCodeAt(0) - 'a'.charCodeAt(0)]++;
        if (cnt[str[end].charCodeAt(0) - 'a'.charCodeAt(0)] == 1)
            distEle++;
 
        // If number of distinct characters is
        // is greater than k, then move starting
        // point of sliding window forward,
        // until count is k.
        if (distEle > k) {
            while (st < end && distEle > k) {
                if (cnt[str[st].charCodeAt(0) - 'a'.charCodeAt(0)] == 1)
                    distEle--;
                cnt[str[st].charCodeAt(0) - 'a'.charCodeAt(0)]--;
                st++;
            }
        }
 
        // Remove characters from the beginning of
        // sliding window having count more than 1
        // to minimize length.
        if (distEle == k) {
            while (st < end && cnt[str[st].charCodeAt(0) - 'a'.charCodeAt(0)] > 1) {
                cnt[str[st].charCodeAt(0) - 'a'.charCodeAt(0)]--;
                st++;
            }
 
            // Compare length with minimum length
            // and update if required.
            currlen = end - st + 1;
            if (currlen < minlen) {
                minlen = currlen;
                startInd = st;
            }
        }
 
        end++;
    }
 
    // Return minimum length  substring.
    return str.substr(startInd, minlen);
}
 
// Driver code
var str = "efecfefd";
var k = 4;
document.write( findMinLenStr(str, k));
 
// This code is contributed by noob2000.
</script>

                    

Output
cfefd

Time Complexity: O(N), where N is the length of the given string. 
Auxiliary Space: O(1)
 



Last Updated : 14 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads