-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTotalSubStringWithKUniqueCharacters.java
More file actions
84 lines (67 loc) · 2.63 KB
/
Copy pathTotalSubStringWithKUniqueCharacters.java
File metadata and controls
84 lines (67 loc) · 2.63 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.leetcode.year_2020.sliding_window;
import com.util.LogUtil;
import java.util.HashMap;
import java.util.Map;
/**
* @author neeraj on 01/07/20
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class TotalSubStringWithKUniqueCharacters {
public static void main(String[] args) {
getTotalSubstringCount("pqpqs", 2);
getTotalSubstringCount("abcabc", 3);
getTotalSubstringCount("aaacb", 3);
getTotalSubstringCount("abc", 3);
getTotalSubStringsCountWithAtMostWay("pqpqs", 2);
getTotalSubStringsCountWithAtMostWay("abcabc", 3);
getTotalSubStringsCountWithAtMostWay("aaacb", 3);
getTotalSubStringsCountWithAtMostWay("abc", 3);
}
public static void getTotalSubStringsCountWithAtMostWay(String str, int k) {
/**
* This is exactly the same problem {@link SubArrayWithKDifferentIntegers}
*/
final int result = substringWithAtMostK(str.toCharArray(), k) - substringWithAtMostK(str.toCharArray(), k-1);
LogUtil.logIt("Total SubString with K Unique Characters via AtMost way.... " + result);
}
private static int substringWithAtMostK(char[] arr, int K) {
final Map<Character, Integer> countMap = new HashMap<>();
int low = 0, high = 0;
int totalSubStrings = 0;
while (high < arr.length) {
countMap.put(arr[high], countMap.getOrDefault(arr[high], 0) + 1);
while (countMap.size() > K) {
// Shrink the window
countMap.put(arr[low], countMap.getOrDefault(arr[low], 0) - 1);
if (countMap.get(arr[low]) == 0) {
countMap.remove(arr[low]);
}
low++;
}
totalSubStrings += high - low + 1;
high++;
}
return totalSubStrings;
}
public static int getTotalSubstringCount(String str, int k) {
int[] count; // Since 26 Alphabets
int totalSubstring = 0;
int distinctCount;
for (int i = 0; i < str.length(); i++) {
distinctCount = 0;
count = new int[26];
for (int j = i; j < str.length(); j++) {
// Check if this item is unique.....
if (count[str.charAt(j) - 'a'] == 0) distinctCount++;
// Increment it's frequency
count[str.charAt(j) - 'a']++;
if (distinctCount == k) {
totalSubstring++;
}
}
}
LogUtil.logIt("Total SubString with K Unique Characters.... " + totalSubstring);
return totalSubstring;
}
}