Shortest Matching Substring

You are given a string s and a pattern string p, where p contains exactly two '*' characters.

The '*' in p matches any sequence of zero or more characters.

Return the length of the shortest substring in s that matches p. If there is no such substring, return -1.

Note: The empty substring is considered valid.

Example 1
Inputs = "abaacbaecebce", p = "ba*c*ce"
Output8
The shortest matching substring of p in s is "baecebce".
Example 2
Inputs = "baccbaadbc", p = "cc*baa*adb"
Output-1
There is no matching substring in s.

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 '*'.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate