Substring Matching Pattern - Problem

You are given a string s and a pattern string p, where p contains exactly one '*' character. The '*' in p can be replaced with any sequence of zero or more characters.

Return true if p can be made a substring of s, and false otherwise.

Note: A substring is a contiguous sequence of characters within a string.

Input & Output

Example 1 — Basic Pattern Match
$ Input: s = "hello", p = "l*e"
Output: true
💡 Note: The pattern 'l*e' can match substring 'lle' by replacing '*' with 'l'. The substring 'lle' exists in 'hello' at positions 2-4.
Example 2 — No Match Possible
$ Input: s = "hello", p = "a*b"
Output: false
💡 Note: There's no way to form a substring of 'hello' using pattern 'a*b' because 'hello' doesn't contain 'a' or 'b'.
Example 3 — Star as Empty String
$ Input: s = "abc", p = "a*c"
Output: true
💡 Note: The pattern 'a*c' matches substring 'ac' by replacing '*' with empty string. 'ac' is a substring of 'abc'.

Constraints

  • 1 ≤ s.length ≤ 1000
  • 1 ≤ p.length ≤ 1000
  • p contains exactly one '*' character
  • s and p consist of lowercase English letters and '*'

Visualization

Tap to expand
Substring Matching: Pattern 'l*e' in String 'hello'helle01234String: "hello"l*eprefixwildcardsuffixPattern 'l*e' matches substring 'lle' (positions 2-4)Result: true
Understanding the Visualization
1
Input
String 'hello' and pattern 'l*e'
2
Process
Find where pattern can match as substring
3
Output
Return true if match exists, false otherwise
Key Takeaway
🎯 Key Insight: Split the pattern at '*' and separately match the prefix and suffix parts with proper positioning
Asked in
Google 25 Amazon 20 Microsoft 15
25.5K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen