From the explanation of the solution for the Longest Substring with K Distinct Characters the below is stated.
The outer
for
loop runs for all characters, and the innerwhile
loop processes each character only once; therefore, the time complexity of the algorithm will be O(N+N)
This refers to these loops.
for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
...
while ((int)charFrequencyMap.size() > k) {
...
}
}
}
My question is if we have a nested loop can it still give linear running time?