desc.html 1.8 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<p>给你两个长度相同的字符串,<code>s</code><code>t</code></p>

<p><code>s</code> 中的第 <code>i</code> 个字符变到 <code>t</code> 中的第 <code>i</code> 个字符需要 <code>|s[i] - t[i]|</code> 的开销(开销可能为 0),也就是两个字符的 ASCII 码值的差的绝对值。</p>

<p>用于变更字符串的最大预算是 <code>maxCost</code>。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。</p>

<p>如果你可以将 <code>s</code> 的子字符串转化为它在 <code>t</code> 中对应的子字符串,则返回可以转化的最大长度。</p>

<p>如果 <code>s</code> 中没有子字符串可以转化成 <code>t</code> 中对应的子字符串,则返回 <code>0</code></p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>s = "abcd", t = "bcdf", maxCost = 3
<strong>输出:</strong>3
<strong>解释:</strong>s<strong> </strong>中的<strong> </strong>"abc" 可以变为 "bcd"。开销为 3,所以最大长度为 3。</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>s = "abcd", t = "cdef", maxCost = 3
<strong>输出:</strong>1
<strong>解释:</strong>s 中的任一字符要想变成 t 中对应的字符,其开销都是 2。因此,最大长度为<code> 1。</code>
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>s = "abcd", t = "acde", maxCost = 0
<strong>输出:</strong>1
<strong>解释:</strong>a -> a, cost = 0,字符串未发生变化,所以最大长度为 1。
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 <= s.length, t.length <= 10^5</code></li>
	<li><code>0 <= maxCost <= 10^6</code></li>
	<li><code>s</code> 和 <code>t</code> 都只含小写英文字母。</li>
</ul>