So I've been working on this for a few days actually. I implemented a solution to the LeetCode problem: 567. Permutation in String
Given two strings s1(queryStr) and s2(sourceStr), return true if s2 contains a permutation of s1, or false otherwise.In other words, return true if one of s1's permutations is thea substring of s2.
- Make it as readable as possible, as if I were doing this at work (we prioritize readability over the typical 1 letter variable-letter-variable nonsense you see in LeetCode submissions).
- It needs to still be fast.
- If itsit's slow, learn why and optimize.
Okay so forFor my algorithm, I ended up using the sliding window method. If you're unfamiliar with that, basically you keep track of a "window" of letters you can actually see. You check if that window is a permutation, and then if not you just slide it up by 1.
queryString size = 38
sourceStr size = 10^810⁸
Average time to find permutation = 11832msqueryString size = 38
sourceStr size = 10^710⁷
Average time to find permutation = 1235msqueryString size = 38
sourceStr size = 10^610⁶
Average time to find permutation = 118msqueryString size = 38
sourceStr size = 10^410⁴
Average time to find permutation = 1msqueryString size = 8
sourceStr size = 10^810⁸
Average time to find permutation = 23ms
All of these test cases pass and the solution is accepted by LeetCode. But, according to LeetCode, my solution falls in the bottom 10% of solutions for CPU speed. And while their method of speed benchmarking is terrible, itsit's consistently at the bottom.
Given the data, its clear that time increases with the size of query string. This makes sense if you look at IsWindowPermutation. I'm iterating the size of window to check whether it is a permutation. This means for small windows itsit's very fast, but for larger windows itsit's slower.