Untitled
unknown
plain_text
a year ago
751 B
4
Indexable
import java.util.HashSet;
public class Solution {
public int countGoodSubstrings(String s, int k) {
int n = s.length();
if (n < k) return 0;
int count = 0;
HashSet<Character> window = new HashSet<>();
for (int i = 0; i < n; i++) {
// Add the current character to the window
window.add(s.charAt(i));
// Remove the character that slides out of the window
if (i >= k) {
window.remove(s.charAt(i - k));
}
// Check if the window size equals k and all characters are unique
if (window.size() == k) {
count++;
}
}
return count;
}
}
Editor is loading...
Leave a Comment