Loading...
Loading...
You are given two strings s and pattern of lowercase English letters.
A window is a substring of s with the same length as pattern. A window matches if it equals pattern, or if it equals pattern after swapping one pair of neighboring characters in pattern.
Return the number of windows that match.
Windows are told apart by their start index, so they may overlap. Each window is counted once, even if it matches in more than one way.
Input: s = "abcacbabc", pattern = "abc"
Output: 3
Explanation: The windows of length 3 are "abc", "bca", "cac", "acb", "cba", "bab", "abc".
The two "abc" windows equal pattern.
"acb" is pattern with its neighbors "b" and "c" swapped.
None of the others can be made from pattern with a single swap of neighbors.
Input: s = "aabab", pattern = "aab"
Output: 2
Explanation: The windows are "aab", "aba", "bab".
"aab" equals pattern. Swapping its two neighboring "a" characters also gives "aab", but the window still counts once.
"aba" is pattern with its neighbors "a" and "b" swapped.
"bab" does not match.
Input: s = "abab", pattern = "b"
Output: 2
Explanation: A single character has no neighbor to swap with, so only the two windows equal to "b" match.
s.length and m= pattern.lengths and pattern consist of lowercase English letters.