1216. Shape-Alike Windows
A cipher analyst looks for passages that share the same letter-repetition shape as a known phrase, ignoring which actual letters are used. Two strings of equal length have the same shape when one can be turned into the other by renaming letters consistently: equal letters must map to equal letters and different letters to different letters. For example abca and xyzx share a shape, but abca and xyzz do not.
Given lowercase strings text and pattern of length m, return the number of windows of text of length m that have the same shape as pattern. Windows are counted by start position and may overlap. Checking each window costs O(n * m); aim for O(n + m) time and space.
Example 1
- Input:
- text = "abab", pattern = "cd"
- Output:
- 3
- Explanation:
Each of the windows
ab,ba,abhas two different letters, likecd, so the answer is 3.
Example 2
- Input:
- text = "aabb", pattern = "cc"
- Output:
- 2
- Explanation:
The windows
aaandbbrepeat one letter likecc, whileabdoes not, so the answer is 2.
Example 3
- Input:
- text = "abcabc", pattern = "xyz"
- Output:
- 4
- Explanation:
All four windows
abc,bca,cab,abchave three different letters, so the answer is 4.
Constraints
1 ≤ pattern.length ≤ text.length ≤ 105
both strings contain only lowercase letters
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n + m)
- Space
- O(n + m)