Skip to main content
Back to problems
Leetcode
Medium
Strings
String Matching
Arrays
Substring Matching Pattern

Determine whether a pattern string can be matched against a text string by comparing a fixed-length substring relationship.

Acceptance 0%
Problem Statement

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 s and p.
  • 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

  • 0s,p0 \le |s|, |p|.
  • The solution should be efficient enough for typical interview-sized inputs.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.