mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			65 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>You are given a string <code>s</code> and a pattern string <code>p</code>, where <code>p</code> contains <strong>exactly two</strong> <code>'*'</code> characters.</p>
 | 
						|
 | 
						|
<p>The <code>'*'</code> in <code>p</code> matches any sequence of zero or more characters.</p>
 | 
						|
 | 
						|
<p>Return the length of the <strong>shortest</strong> <span data-keyword="substring">substring</span> in <code>s</code> that matches <code>p</code>. If there is no such substring, return -1.</p>
 | 
						|
<strong>Note:</strong> The empty substring is considered valid.
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">s = "abaacbaecebce", p = "ba*c*ce"</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">8</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>The shortest matching substring of <code>p</code> in <code>s</code> is <code>"<u><strong>ba</strong></u>e<u><strong>c</strong></u>eb<u><strong>ce</strong></u>"</code>.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">s = "baccbaadbc", p = "cc*baa*adb"</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>There is no matching substring in <code>s</code>.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 3:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">s = "a", p = "**"</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">0</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>The empty substring is the shortest matching substring.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 4:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">s = "madlogic", p = "*adlogi*"</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">6</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>The shortest matching substring of <code>p</code> in <code>s</code> is <code>"<strong><u>adlogi</u></strong>"</code>.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
 | 
						|
	<li><code>2 <= p.length <= 10<sup>5</sup></code></li>
 | 
						|
	<li><code>s</code> contains only lowercase English letters.</li>
 | 
						|
	<li><code>p</code> contains only lowercase English letters and exactly two <code>'*'</code>.</li>
 | 
						|
</ul>
 |