← 返回 apple 的题目列表Substring Pattern Matches
类型:qbank
Given a string s and a non-empty pattern pattern, return every starting index i in s where the substring s[i..i + pattern.length - 1] equals pattern.
Examples
Example 1:
Input: s = "abcabc", pattern = "abc"
Output: [0, 3]
Explanation:
The pattern appears at positions 0 and 3.
Example 2:
Input: s = "aaaa", pattern = "aa"
Output: [0, 1, 2]
Explanation:
Overlapping matches count — aa starts at indices 0, 1, and 2.
Example 3:
Input: s = "hello", pattern = "xyz"
Output: []
Explanation:
No substring of s equals pattern.
Constraints
0 <= s.length <= 10^4
1 <= pattern.length <= 10^3
s and pattern consist of printable ASCII characters