Hi leetcoders, want to understand if my assumption is right for the sliding window time complexity
I will take one example here
https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
public int lengthOfLongestSubstringKDistinct(String s, int k) {
Map<Character,Integer> map = new HashMap<>();
int start = 0, end = 0, counter = 0, len = 0;
while(end < s.length()){
char c = s.charAt(end);
map.put(c, map.getOrDefault(c, 0) + 1);
if(map.get(c) == 1)
counter++;//new char
end++;
while(counter > k){
char cTemp = s.charAt(start);
map.put(cTemp, map.get(cTemp) - 1);
if(map.get(cTemp) == 0){
counter--;
}
start++;
}
len = Math.max(len, end-start);
}
return len;
}Time complexity for above would be O(N * K) right? N is the length of the string and k which is technically a constant and we have a inner while loop <while(counter > k)> which moves the 1st pointer
I couldn't find a proper answer in discussion so wanted to post here.
General template for silding window below
https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/92007/Sliding-Window-algorithm-template-to-solve-all-the-Leetcode-substring-search-problem.
Please help me understand this :)