Brutal force works - and a smarter way is to utilize KMP. The most popular two solutions are essentially the same - one is obvious KMP, and another one is https://discuss.leetcode.com/topic/68206/easy-python-solution-with-explaination , which simplifies the code a lot - bravo!