Substring with Concatenation of All Words

Solution Code
if word in word_counts:
                    curr_counts[word] = curr_counts.get(word, 0) + 1
                    count += 1
                    
                    while curr_counts[word] > word_counts[word]:
                        left_word = s[left : left + word_len]
                        curr_counts[left_word] -= 1
                        count -= 1
                        left += word_len
                    
                    if count == num_words:
                        res.append(left)
                else:
                    curr_counts.clear()
                    count = 0
                    left = right
        return res

Time Complexity

O(N L)

Space Complexity

O(M L)