public class Solution {
/**
* @param stringIn: The original string.
* @param K: The length of substrings.
* @return: return the count of substring of length K and exactly K distinct characters.
*/
public int KSubstring(String stringIn, int K) {
if (stringIn == null || stringIn.length() == 0 || K <= 0) {
return 0;
}
int count = 0;
HashMap<Character, Integer> charMap = new HashMap<>();
HashSet<String> resultSet = new HashSet<String>();
int len = stringIn.length();
int j = 0;
for (int i = 0; i < len; i++) {
while (j < len && charMap.size() < K) {`
char c = stringIn.charAt(j);
if (charMap.containsKey(c)) {
break;
}
charMap.put(c, 1);
j++;
if (charMap.size() == K) {
resultSet.add(stringIn.substring(i, j));
}
}
charMap.remove(stringIn.charAt(i));
}
return resultSet.size();
}
}
Sliding Window - Two Pointer
Two pointer O(N)
create two hashSet, One set to store Character, One Set to store the substring
i start index, test every start index, to check whether it can be a substring of length K and containing different characters
if contains duplicate character , break, start from next index
public class Solution {
/**
* @param stringIn: The original string.
* @param K: The length of substrings.
* @return: return the count of substring of length K and exactly K distinct characters.
*/
public int KSubstring(String stringIn, int K) {
if (stringIn == null || stringIn.length() == 0 || K <= 0) {
return 0;
}
int count = 0;
HashSet<Character> charSet = new HashSet<>();
HashSet<String> resultSet = new HashSet<String>();
int len = stringIn.length();
int j = 0;
for (int i = 0; i <= len - K; i++) {
while (j < len && charSet.size() < K) {
char c = stringIn.charAt(j);
if (!charSet.contains(c)) {
charSet.add(c);
} else {
break;
}
j++;
if (charSet.size() == K) {
resultSet.add(stringIn.substring(i, j));
}
}
charSet.remove(stringIn.charAt(i));
}
return resultSet.size();
}
}