mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			123 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>You are given a string <code>s</code> of length <code>n</code> and an integer array <code>order</code>, where <code>order</code> is a <strong><span data-keyword="permutation">permutation</span></strong> of the numbers in the range <code>[0, n - 1]</code>.</p>
 | 
						|
 | 
						|
<p>Starting from time <code>t = 0</code>, replace the character at index <code>order[t]</code> in <code>s</code> with <code>'*'</code> at each time step.</p>
 | 
						|
 | 
						|
<p>A <strong><span data-keyword="substring-nonempty">substring</span></strong> is <strong>valid</strong> if it contains <strong>at least</strong> one <code>'*'</code>.</p>
 | 
						|
 | 
						|
<p>A string is <strong>active</strong> if the total number of <strong>valid</strong> substrings is greater than or equal to <code>k</code>.</p>
 | 
						|
 | 
						|
<p>Return the <strong>minimum</strong> time <code>t</code> at which the string <code>s</code> becomes <strong>active</strong>. If it is impossible, return -1.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">s = "abc", order = [1,0,2], k = 2</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">0</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<table style="border: 1px solid black;">
 | 
						|
	<thead>
 | 
						|
		<tr>
 | 
						|
			<th style="border: 1px solid black;"><code>t</code></th>
 | 
						|
			<th style="border: 1px solid black;"><code>order[t]</code></th>
 | 
						|
			<th style="border: 1px solid black;">Modified <code>s</code></th>
 | 
						|
			<th style="border: 1px solid black;">Valid Substrings</th>
 | 
						|
			<th style="border: 1px solid black;">Count</th>
 | 
						|
			<th style="border: 1px solid black;">Active<br />
 | 
						|
			(Count >= k)</th>
 | 
						|
		</tr>
 | 
						|
	</thead>
 | 
						|
	<tbody>
 | 
						|
		<tr>
 | 
						|
			<td style="border: 1px solid black;">0</td>
 | 
						|
			<td style="border: 1px solid black;">1</td>
 | 
						|
			<td style="border: 1px solid black;"><code>"a*c"</code></td>
 | 
						|
			<td style="border: 1px solid black;"><code>"*"</code>, <code>"a*"</code>, <code>"*c"</code>, <code>"a*c"</code></td>
 | 
						|
			<td style="border: 1px solid black;">4</td>
 | 
						|
			<td style="border: 1px solid black;">Yes</td>
 | 
						|
		</tr>
 | 
						|
	</tbody>
 | 
						|
</table>
 | 
						|
 | 
						|
<p>The string <code>s</code> becomes active at <code>t = 0</code>. Thus, the answer is 0.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">s = "cat", order = [0,2,1], k = 6</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">2</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<table style="border: 1px solid black;">
 | 
						|
	<thead>
 | 
						|
		<tr>
 | 
						|
			<th style="border: 1px solid black;"><code>t</code></th>
 | 
						|
			<th style="border: 1px solid black;"><code>order[t]</code></th>
 | 
						|
			<th style="border: 1px solid black;">Modified <code>s</code></th>
 | 
						|
			<th style="border: 1px solid black;">Valid Substrings</th>
 | 
						|
			<th style="border: 1px solid black;">Count</th>
 | 
						|
			<th style="border: 1px solid black;">Active<br />
 | 
						|
			(Count >= k)</th>
 | 
						|
		</tr>
 | 
						|
	</thead>
 | 
						|
	<tbody>
 | 
						|
		<tr>
 | 
						|
			<td style="border: 1px solid black;">0</td>
 | 
						|
			<td style="border: 1px solid black;">0</td>
 | 
						|
			<td style="border: 1px solid black;"><code>"*at"</code></td>
 | 
						|
			<td style="border: 1px solid black;"><code>"*"</code>, <code>"*a"</code>, <code>"*at"</code></td>
 | 
						|
			<td style="border: 1px solid black;">3</td>
 | 
						|
			<td style="border: 1px solid black;">No</td>
 | 
						|
		</tr>
 | 
						|
		<tr>
 | 
						|
			<td style="border: 1px solid black;">1</td>
 | 
						|
			<td style="border: 1px solid black;">2</td>
 | 
						|
			<td style="border: 1px solid black;"><code>"*a*"</code></td>
 | 
						|
			<td style="border: 1px solid black;"><code>"*"</code>, <code>"*a"</code>, <code>"<code inline="">*a*"</code></code>, <code>"<code inline="">a*"</code></code>, <code>"*"</code></td>
 | 
						|
			<td style="border: 1px solid black;">5</td>
 | 
						|
			<td style="border: 1px solid black;">No</td>
 | 
						|
		</tr>
 | 
						|
		<tr>
 | 
						|
			<td style="border: 1px solid black;">2</td>
 | 
						|
			<td style="border: 1px solid black;">1</td>
 | 
						|
			<td style="border: 1px solid black;"><code>"***"</code></td>
 | 
						|
			<td style="border: 1px solid black;">All substrings (contain <code>'*'</code>)</td>
 | 
						|
			<td style="border: 1px solid black;">6</td>
 | 
						|
			<td style="border: 1px solid black;">Yes</td>
 | 
						|
		</tr>
 | 
						|
	</tbody>
 | 
						|
</table>
 | 
						|
 | 
						|
<p>The string <code>s</code> becomes active at <code>t = 2</code>. Thus, the answer is 2.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 3:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">s = "xy", order = [0,1], k = 4</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>Even after all replacements, it is impossible to obtain <code>k = 4</code> valid substrings. Thus, the answer is -1.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= n == s.length <= 10<sup>5</sup></code></li>
 | 
						|
	<li><code>order.length == n</code></li>
 | 
						|
	<li><code>0 <= order[i] <= n - 1</code></li>
 | 
						|
	<li><code>s</code> consists of lowercase English letters.</li>
 | 
						|
	<li><code>order</code> is a permutation of integers from 0 to <code>n - 1</code>.</li>
 | 
						|
	<li><code>1 <= k <= 10<sup>9</sup></code></li>
 | 
						|
</ul>
 |