Skip to main content
Back to problems
#3407
Easy Algorithms

Substring matching pattern

String String Matching
28.3% acceptance
Feb 25, 2026
116
53
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn has_match(s: String, p: String) -> bool {
    let star_pos = p.find('*').unwrap();
    let prefix = &p[..star_pos];
    let suffix = &p[star_pos + 1..];
    if let Some(idx) = s.find(prefix) {
      s[idx + prefix.len()..].contains(suffix)
    } else {
      false
    }
  }
}