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++
#include <bits/stdc++.h>
using namespace std;
string findMinLenStr(string& s, int k)
{
int n = s.length();
string result = "" ;
int minn = INT_MAX;
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];
if (unmap.size() == k) {
if (j - i + 1 < minn) {
minn = j - i + 1;
result = s2;
}
break ;
}
}
}
return result;
}
int main()
{
string str = "ababcb" ;
int k = 3;
cout << findMinLenStr(str, k);
return 0;
}
|
Java
import java.util.*;
class Main {
public static String findMinLenStr(String s, int k) {
int n = s.length();
String result = "" ;
int minn = Integer.MAX_VALUE;
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));
if (unmap.size() == k) {
if (j - i + 1 < minn) {
minn = j - i + 1 ;
result = s2.toString();
}
break ;
}
}
}
return result;
}
public static void main(String[] args) {
String str = "ababcb" ;
int k = 3 ;
System.out.println(findMinLenStr(str, k));
}
}
|
Python3
import sys
def findMinLenStr( s, k):
n = len (s);
result = "";
minn = sys.maxsize;
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];
if ( len (unmap) = = k) :
if (j - i + 1 < minn) :
minn = j - i + 1 ;
result = s2;
break ;
return result;
str = "ababcb" ;
k = 3 ;
print (findMinLenStr( str , k));
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static string findMinLenStr( string s, int k)
{
int n = s.Length;
string result = "" ;
int minn = Int32.MaxValue;
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];
if (unmap.Count == k) {
if (j - i + 1 < minn) {
minn = j - i + 1;
result = s2;
}
break ;
}
}
}
return result;
}
public static void Main ( string [] args)
{
string str = "ababcb" ;
int k = 3;
Console.Write(findMinLenStr(str, k));
}
}
|
Javascript
function findMinLenStr(s, k)
{
let n = s.length;
let result = "" ;
let minn = Number.MAX_SAFE_INTEGER;
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];
if (unmap.size == k) {
if (j - i + 1 < minn) {
minn = j - i + 1;
result = s2;
}
break ;
}
}
}
return result;
}
let str = "ababcb" ;
let k = 3;
document.write(findMinLenStr(str, k));
|
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++
#include <bits/stdc++.h>
using namespace std;
string findMinLenStr(string str, int k)
{
int n = str.length();
int st = 0;
int end = 0;
int cnt[26];
memset (cnt, 0, sizeof (cnt));
int distEle = 0;
int currlen;
int minlen = n;
int startInd = -1;
while (end < n) {
cnt[str[end] - 'a' ]++;
if (cnt[str[end] - 'a' ] == 1)
distEle++;
if (distEle > k) {
while (st < end && distEle > k) {
if (cnt[str[st] - 'a' ] == 1)
distEle--;
cnt[str[st] - 'a' ]--;
st++;
}
}
if (distEle == k) {
while (st < end && cnt[str[st] - 'a' ] > 1) {
cnt[str[st] - 'a' ]--;
st++;
}
currlen = end - st + 1;
if (currlen < minlen) {
minlen = currlen;
startInd = st;
}
}
end++;
}
return str.substr(startInd, minlen);
}
int main()
{
string str = "efecfefd" ;
int k = 4;
cout << findMinLenStr(str, k);
return 0;
}
|
Java
class GFG
{
static String findMinLenStr(String str, int k)
{
int n = str.length();
int st = 0 ;
int end = 0 ;
int cnt[] = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++)cnt[i] = 0 ;
int distEle = 0 ;
int currlen;
int minlen = n;
int startInd = - 1 ;
while (end < n)
{
cnt[str.charAt(end) - 'a' ]++;
if (cnt[str.charAt(end) - 'a' ] == 1 )
distEle++;
if (distEle > k)
{
while (st < end && distEle > k)
{
if (cnt[str.charAt(st) - 'a' ] == 1 )
distEle--;
cnt[str.charAt(st) - 'a' ]--;
st++;
}
}
if (distEle == k)
{
while (st < end && cnt[str.charAt(st) - 'a' ] > 1 )
{
cnt[str.charAt(st) - 'a' ]--;
st++;
}
currlen = end - st + 1 ;
if (currlen < minlen)
{
minlen = currlen;
startInd = st;
}
}
end++;
}
return str.substring(startInd,startInd + minlen);
}
public static void main(String args[])
{
String str = "efecfefd" ;
int k = 4 ;
System.out.println(findMinLenStr(str, k));
}
}
|
Python 3
def findMinLenStr( str , k):
n = len ( str )
st = 0
end = 0
cnt = [ 0 ] * 26
distEle = 0
currlen = 0
minlen = n
startInd = - 1
while (end < n):
cnt[ ord ( str [end]) - ord ( 'a' )] + = 1
if (cnt[ ord ( str [end]) - ord ( 'a' )] = = 1 ):
distEle + = 1
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
if (distEle = = k):
while (st < end and cnt[ ord ( str [st]) -
ord ( 'a' )] > 1 ):
cnt[ ord ( str [st]) - ord ( 'a' )] - = 1
st + = 1
currlen = end - st + 1
if (currlen < minlen):
minlen = currlen
startInd = st
end + = 1
return str [startInd : startInd + minlen]
if __name__ = = "__main__" :
str = "efecfefd"
k = 4
print (findMinLenStr( str , k))
|
C#
using System;
class GFG
{
static String findMinLenStr( string str, int k)
{
int n = str.Length;
int st = 0;
int end = 0;
int []cnt = new int [26];
for ( int i = 0; i < 26; i++)cnt[i] = 0;
int distEle = 0;
int currlen;
int minlen = n;
int startInd = -1;
while (end < n)
{
cnt[str[end] - 'a' ]++;
if (cnt[str[end] - 'a' ] == 1)
distEle++;
if (distEle > k)
{
while (st < end && distEle > k)
{
if (cnt[str[st] - 'a' ] == 1)
distEle--;
cnt[str[st] - 'a' ]--;
st++;
}
}
if (distEle == k)
{
while (st < end && cnt[str[st] - 'a' ] > 1)
{
cnt[str[st] - 'a' ]--;
st++;
}
currlen = end - st + 1;
if (currlen < minlen)
{
minlen = currlen;
startInd = st;
}
}
end++;
}
return str.Substring(startInd, minlen);
}
public static void Main()
{
string str = "efecfefd" ;
int k = 4;
Console.WriteLine(findMinLenStr(str, k));
}
}
|
Javascript
<script>
function findMinLenStr(str, k)
{
var n = str.length;
var st = 0;
var end = 0;
var cnt = Array(26).fill(0);
var distEle = 0;
var currlen;
var minlen = n;
var startInd = -1;
while (end < n) {
cnt[str[end].charCodeAt(0) - 'a' .charCodeAt(0)]++;
if (cnt[str[end].charCodeAt(0) - 'a' .charCodeAt(0)] == 1)
distEle++;
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++;
}
}
if (distEle == k) {
while (st < end && cnt[str[st].charCodeAt(0) - 'a' .charCodeAt(0)] > 1) {
cnt[str[st].charCodeAt(0) - 'a' .charCodeAt(0)]--;
st++;
}
currlen = end - st + 1;
if (currlen < minlen) {
minlen = currlen;
startInd = st;
}
}
end++;
}
return str.substr(startInd, minlen);
}
var str = "efecfefd" ;
var k = 4;
document.write( findMinLenStr(str, k));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Feb, 2023
Like Article
Save Article