-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14.java
More file actions
59 lines (49 loc) · 1.92 KB
/
Copy pathDay14.java
File metadata and controls
59 lines (49 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.HashMap;
public class SubstringsWithKDistinct {
// Function to count substrings with exactly k distinct characters
public static int countSubstringsWithKDistinct(String s, int k) {
if (s == null || s.length() == 0 || k > s.length()) {
return 0;
}
return atMostKDistinct(s, k) - atMostKDistinct(s, k - 1);
}
// Helper function to count substrings with at most k distinct characters
private static int atMostKDistinct(String s, int k) {
int left = 0, count = 0;
HashMap<Character, Integer> map = new HashMap<>();
for (int right = 0; right < s.length(); right++) {
char ch = s.charAt(right);
map.put(ch, map.getOrDefault(ch, 0) + 1);
// Shrink the window if more than k distinct characters
while (map.size() > k) {
char leftChar = s.charAt(left);
map.put(leftChar, map.get(leftChar) - 1);
if (map.get(leftChar) == 0) {
map.remove(leftChar);
}
left++;
}
// Add substrings ending at 'right'
count += right - left + 1;
}
return count;
}
// Main method to test
public static void main(String[] args) {
String s1 = "pqpqs";
int k1 = 2;
System.out.println(countSubstringsWithKDistinct(s1, k1)); // Output: 7
String s2 = "aabacbebebe";
int k2 = 3;
System.out.println(countSubstringsWithKDistinct(s2, k2)); // Output: 10
String s3 = "a";
int k3 = 1;
System.out.println(countSubstringsWithKDistinct(s3, k3)); // Output: 1
String s4 = "abc";
int k4 = 3;
System.out.println(countSubstringsWithKDistinct(s4, k4)); // Output: 1
String s5 = "abc";
int k5 = 2;
System.out.println(countSubstringsWithKDistinct(s5, k5)); // Output: 2
}
}