1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/两个字符串的切换距离 [shift-distance-between-two-strings].html
2024-11-29 17:49:27 +08:00

60 lines
3.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你两个长度相同的字符串&nbsp;<code>s</code>&nbsp;<code>t</code>&nbsp;,以及两个整数数组&nbsp;<code>nextCost</code>&nbsp;<code>previousCost</code>&nbsp;</p>
<p>一次操作中,你可以选择 <code>s</code>&nbsp;中的一个下标 <code>i</code>&nbsp;,执行以下操作 <strong>之一</strong>&nbsp;</p>
<ul>
<li>&nbsp;<code>s[i]</code>&nbsp;切换为字母表中的下一个字母,如果&nbsp;<code>s[i] == 'z'</code>&nbsp;,切换后得到&nbsp;<code>'a'</code>&nbsp;。操作的代价为&nbsp;<code>nextCost[j]</code>&nbsp;,其中&nbsp;<code>j</code>&nbsp;表示&nbsp;<code>s[i]</code>&nbsp;在字母表中的下标。</li>
<li>&nbsp;<code>s[i]</code>&nbsp;切换为字母表中的上一个字母,如果&nbsp;<code>s[i] == 'a'</code>&nbsp;,切换后得到&nbsp;<code>'z'</code>&nbsp;。操作的代价为&nbsp;<code>previousCost[j]</code>&nbsp;,其中&nbsp;<code>j</code>&nbsp;<code>s[i]</code>&nbsp;在字母表中的下标。</li>
</ul>
<p><strong>切换距离</strong>&nbsp;指的是将字符串 <code>s</code>&nbsp;变为字符串 <code>t</code>&nbsp;<strong>最少</strong>&nbsp;操作代价总和。</p>
<p>请你返回从 <code>s</code>&nbsp;<code>t</code>&nbsp;<strong>切换距离</strong>&nbsp;</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]</span></p>
<p><span class="example-io"><b>输出:</b>2</span></p>
<p><b>解释:</b></p>
<ul>
<li>选择下标&nbsp;<code>i = 0</code>&nbsp;并将&nbsp;<code>s[0]</code>&nbsp;向前切换 25 次,总代价为 1 。</li>
<li>选择下标&nbsp;<code>i = 1</code>&nbsp;并将&nbsp;<code>s[1]</code>&nbsp;向后切换 25 次,总代价为 0 。</li>
<li>选择下标&nbsp;<code>i = 2</code>&nbsp;并将&nbsp;<code>s[2]</code>&nbsp;向前切换 25 次,总代价为 1 。</li>
<li>选择下标&nbsp;<code>i = 3</code>&nbsp;并将&nbsp;<code>s[3]</code>&nbsp;向后切换 25 次,总代价为 0 。</li>
</ul>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]</span></p>
<p><span class="example-io"><b>输出:</b>31</span></p>
<p><b>解释:</b></p>
<ul>
<li>选择下标&nbsp;<code>i = 0</code>&nbsp;并将&nbsp;<code>s[0]</code>&nbsp;向前切换 9 次,总代价为 9 。</li>
<li>选择下标&nbsp;<code>i = 1</code>&nbsp;并将&nbsp;<code>s[1]</code>&nbsp;向后切换 10 次,总代价为 10 。</li>
<li>选择下标&nbsp;<code>i = 2</code> 并将&nbsp;<code>s[2]</code>&nbsp;向前切换 1 次,总代价为 1 。</li>
<li>选择下标 <code>i = 3</code> 并将&nbsp;<code>s[3]</code>&nbsp;向后切换 11 次,总代价为 11 。</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length == t.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code>&nbsp;<code>t</code>&nbsp;都只包含小写英文字母。</li>
<li><code>nextCost.length == previousCost.length == 26</code></li>
<li><code>0 &lt;= nextCost[i], previousCost[i] &lt;= 10<sup>9</sup></code></li>
</ul>