LeetCode 题解工作台
两个字符串的切换距离
给你两个长度相同的字符串 s 和 t ,以及两个整数数组 nextCost 和 previousCost 。 一次操作中,你可以选择 s 中的一个下标 i ,执行以下操作 之一 : 将 s[i] 切换为字母表中的下一个字母,如果 s[i] == 'z' ,切换后得到 'a' 。操作的代价为 next…
3
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数组·string
答案摘要
class Solution: def shiftDistance(
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·string 题型思路
题目描述
给你两个长度相同的字符串 s 和 t ,以及两个整数数组 nextCost 和 previousCost 。
一次操作中,你可以选择 s 中的一个下标 i ,执行以下操作 之一 :
- 将
s[i]切换为字母表中的下一个字母,如果s[i] == 'z',切换后得到'a'。操作的代价为nextCost[j],其中j表示s[i]在字母表中的下标。 - 将
s[i]切换为字母表中的上一个字母,如果s[i] == 'a',切换后得到'z'。操作的代价为previousCost[j],其中j是s[i]在字母表中的下标。
切换距离 指的是将字符串 s 变为字符串 t 的 最少 操作代价总和。
请你返回从 s 到 t 的 切换距离 。
示例 1:
输入: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]
输出:2
解释:
- 选择下标
i = 0并将s[0]向前切换 25 次,总代价为 1 。 - 选择下标
i = 1并将s[1]向后切换 25 次,总代价为 0 。 - 选择下标
i = 2并将s[2]向前切换 25 次,总代价为 1 。 - 选择下标
i = 3并将s[3]向后切换 25 次,总代价为 0 。
示例 2:
输入: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]
输出:31
解释:
- 选择下标
i = 0并将s[0]向前切换 9 次,总代价为 9 。 - 选择下标
i = 1并将s[1]向后切换 10 次,总代价为 10 。 - 选择下标
i = 2并将s[2]向前切换 1 次,总代价为 1 。 - 选择下标
i = 3并将s[3]向后切换 11 次,总代价为 11 。
提示:
1 <= s.length == t.length <= 105s和t都只包含小写英文字母。nextCost.length == previousCost.length == 260 <= nextCost[i], previousCost[i] <= 109
解题思路
方法一
class Solution:
def shiftDistance(
self, s: str, t: str, nextCost: List[int], previousCost: List[int]
) -> int:
m = 26
s1 = [0] * (m << 1 | 1)
s2 = [0] * (m << 1 | 1)
for i in range(m << 1):
s1[i + 1] = s1[i] + nextCost[i % m]
s2[i + 1] = s2[i] + previousCost[(i + 1) % m]
ans = 0
for a, b in zip(s, t):
x, y = ord(a) - ord("a"), ord(b) - ord("a")
c1 = s1[y + m if y < x else y] - s1[x]
c2 = s2[x + m if x < y else x] - s2[y]
ans += min(c1, c2)
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate understands the need for efficient transformations between characters.
- question_mark
Candidate can identify the importance of cost minimization in this string problem.
- question_mark
Candidate is familiar with the optimal use of dynamic programming to solve cost-related problems.
常见陷阱
外企场景- error
Misunderstanding the directionality of the shift costs can lead to incorrect calculations.
- error
Failing to optimize for space or time complexity when dealing with large inputs.
- error
Not considering edge cases where no transformations are needed, which can affect the solution's correctness.
进阶变体
外企场景- arrow_right_alt
Consider the variant where the `nextCost` and `previousCost` arrays are not provided. How would you approach the problem?
- arrow_right_alt
What if the strings `s` and `t` have different lengths? How would the problem change?
- arrow_right_alt
How would the problem change if we were allowed to perform shifts on multiple characters at once?