Determine whether a pattern string can be matched against a text string by comparing a fixed-length substring relationship.
Problem
You are given two strings: a text string s and a pattern string p.
Your task is to determine whether there exists a way to match p to some substring of s according to the problem's substring-matching rule. In the common interview formulation, this means checking whether p occurs in s as a contiguous substring or whether two strings can be aligned so that the pattern matches a substring of the text.
Return whether such a match exists.
Notes
- Matching must be contiguous.
- The exact comparison rule is case-sensitive.
- If the pattern is longer than the text, the answer is false.
Input Format
- Two strings
sandp. - Both strings contain standard printable characters unless otherwise specified by the platform.
Output Format
- Return a boolean indicating whether the pattern can be matched to a substring of the text.
Constraints
- .
- The solution should be efficient enough for typical interview-sized inputs.
Example 1
Input
s = "leetcode" p = "code"
Output
true
Explanation
The substring "code" appears contiguously inside "leetcode".
Example 2
Input
s = "hello" p = "world"
Output
false
Explanation
No contiguous substring of "hello" matches "world".
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.