Turn each window of the text into a number, a hash, and compare numbers instead of strings. Sliding the window one place updates the hash with a subtraction, a multiplication and an addition, so most windows are dismissed in one step. Letters are checked only when the hashes agree.
- Average time
- O(n + m)
- Space
- O(1)