0% found this document useful (0 votes)
13 views

Minimum Window Count With K Distinct Characters

The document contains a Java program that finds the minimum window size of a string that contains at least k unique characters. It uses a sliding window approach with two pointers and a HashMap to track unique character counts.

Uploaded by

Prateek Saha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Minimum Window Count With K Distinct Characters

The document contains a Java program that finds the minimum window size of a string that contains at least k unique characters. It uses a sliding window approach with two pointers and a HashMap to track unique character counts.

Uploaded by

Prateek Saha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

*;
class Solution {
public static void findCount(String s, int k){
int l = s.length();
int i=-1, j = -1;
int mn = Integer.MAX_VALUE;
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
while (i< (l-1)){
i++;
char ch = s.charAt(i);
map.put(ch,map.getOrDefault(ch,0)+1);
System.out.print("addition to map");
for (Character key: map.keySet()) {
System.out.println(key);
System.out.println(map.get(key));
}
System.out.println(" i +" + i);
while(j<i && map.size()>k){
mn = Math.min(mn, i-(j+1));
j++;
System.out.println(" j+" + j);
ch = s.charAt(j);
if (map.get(ch) ==1) {
map.remove(ch);
} else {
map.put(ch,map.get(ch)-1);
}
System.out.println("deletion from map");
for (Character key: map.keySet()) {
System.out.println(key);
System.out.println(map.get(key));
}
}
}
System.out.println("minimum window ="+mn);
}
public static void main(String args[]){
String s1 = "abbdefgg";
int k = 3;
findCount(s1,k);
}
}

You might also like