mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			41 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>text</code> 和另一个下标从 <strong>0</strong> 开始且长度为 <code>2</code> 的字符串 <code>pattern</code> ,两者都只包含小写英文字母。</p>
 | 
						||
 | 
						||
<p>你可以在 <code>text</code> 中任意位置插入 <strong>一个</strong> 字符,这个插入的字符必须是 <code>pattern[0]</code> <b>或者</b> <code>pattern[1]</code> 。注意,这个字符可以插入在 <code>text</code> 开头或者结尾的位置。</p>
 | 
						||
 | 
						||
<p>请你返回插入一个字符后,<code>text</code> 中最多包含多少个等于 <code>pattern</code> 的 <strong>子序列</strong> 。</p>
 | 
						||
 | 
						||
<p><strong>子序列</strong> 指的是将一个字符串删除若干个字符后(也可以不删除),剩余字符保持原本顺序得到的字符串。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>text = "abdcdbc", pattern = "ac"
 | 
						||
<b>输出:</b>4
 | 
						||
<strong>解释:</strong>
 | 
						||
如果我们在 text[1] 和 text[2] 之间添加 pattern[0] = 'a' ,那么我们得到 "ab<em><strong>a</strong></em>dcdbc" 。那么 "ac" 作为子序列出现 4 次。
 | 
						||
其他得到 4 个 "ac" 子序列的方案还有 "<em><strong>a</strong></em>abdcdbc" 和 "abd<em><strong>a</strong></em>cdbc" 。
 | 
						||
但是,"abdc<em><strong>a</strong></em>dbc" ,"abd<em><strong>c</strong></em>cdbc" 和 "abdcdbc<em><strong>c</strong></em>" 这些字符串虽然是可行的插入方案,但是只出现了 3 次 "ac" 子序列,所以不是最优解。
 | 
						||
可以证明插入一个字符后,无法得到超过 4 个 "ac" 子序列。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>text = "aabb", pattern = "ab"
 | 
						||
<b>输出:</b>6
 | 
						||
<strong>解释:</strong>
 | 
						||
可以得到 6 个 "ab" 子序列的部分方案为 "<em><strong>a</strong></em>aabb" ,"aa<em><strong>a</strong></em>bb" 和 "aab<em><strong>b</strong></em>b" 。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= text.length <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>pattern.length == 2</code></li>
 | 
						||
	<li><code>text</code> 和 <code>pattern</code> 都只包含小写英文字母。</li>
 | 
						||
</ul>
 |