Skip to content

Practice Shortest Matching Substring

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3455
LeetCode Problem

Shortest Matching Substring

Problem Statement

Given s and a pattern p containing exactly two asterisks, each asterisk matches any sequence of zero or more characters. Return the length of the shortest substring of s matching p, or -1 if none exists. The empty substring is allowed.

Source: Shortest Matching Substring on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "abaacbaecebce", p = "ba*c*ce"
Output
8

# Example 2

Input
s = "baccbaadbc", p = "cc*baa*adb"
Output
-1

# Example 3

Input
s = "a", p = "**"
Output
0

# Example 4

Input
s = "madlogic", p = "*adlogi*"
Output
6

Constraints

1 <= s.length <= 10^5
2 <= p.length <= 10^5
s contains only lowercase English letters.
p contains only lowercase English letters and exactly two '*'.

Related LeetCode Problems

Practice more problems similar to Shortest Matching Substring.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.