@werlay Sir, I have to tell you that I've been in your situation exactly one year ago. Our teacher gave us as an assignment almost the same task. Most of my classmates didn't solve it, because they tried to use a lot of complicated things. I, instead, used the KMP algorithm for pattern matching. Not only did I get a 10, but also my teacher congratulated me on using the easiest/most optimal way possible.
Comparing the two methods, the KMP, and your method, in my opinion, the KMP is more easier to understand. If you wish, as I said before, I can give you full explanations on the KMP, or if you wish, I can guide you through the same tutorials I've used when I first learnt this algorithm.
At least at my school, using already defined algorithms is compulsory. And by learning those algorithms, you actually understand what their "inventor" was thinking of, what his idea was, and you finally develop a good algorithmic thinking, being able to solve further problems without difficulties.
I really hope I could help you,
~ Raul ~