3361. Shift Distance Between Two Strings Medium

@problem@discussion
#Array#String#Prefix Sum



1/**
2 * [3361] Shift Distance Between Two Strings
3 *
4 * You are given two strings s and t of the same length, and two integer arrays nextCost and previousCost.
5 * In one operation, you can pick any index i of s, and perform either one of the following actions:
6 * 
7 * 	Shift s[i] to the next letter in the alphabet. If s[i] == 'z', you should replace it with 'a'. This operation costs nextCost[j] where j is the index of s[i] in the alphabet.
8 * 	Shift s[i] to the previous letter in the alphabet. If s[i] == 'a', you should replace it with 'z'. This operation costs previousCost[j] where j is the index of s[i] in the alphabet.
9 * 
10 * The shift distance is the minimum total cost of operations required to transform s into t.
11 * Return the shift distance from s to t.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">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>
16 * Output: <span class="example-io">2</span>
17 * Explanation:
18 * 
19 * 	We choose index i = 0 and shift s[0] 25 times to the previous character for a total cost of 1.
20 * 	We choose index i = 1 and shift s[1] 25 times to the next character for a total cost of 0.
21 * 	We choose index i = 2 and shift s[2] 25 times to the previous character for a total cost of 1.
22 * 	We choose index i = 3 and shift s[3] 25 times to the next character for a total cost of 0.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">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>
27 * Output: <span class="example-io">31</span>
28 * Explanation:
29 * 
30 * 	We choose index i = 0 and shift s[0] 9 times to the previous character for a total cost of 9.
31 * 	We choose index i = 1 and shift s[1] 10 times to the next character for a total cost of 10.
32 * 	We choose index i = 2 and shift s[2] 1 time to the previous character for a total cost of 1.
33 * 	We choose index i = 3 and shift s[3] 11 times to the next character for a total cost of 11.
34 * </div>
35 *  
36 * Constraints:
37 * 
38 * 	1 <= s.length == t.length <= 10^5
39 * 	s and t consist only of lowercase English letters.
40 * 	nextCost.length == previousCost.length == 26
41 * 	0 <= nextCost[i], previousCost[i] <= 10^9
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/shift-distance-between-two-strings/
47// discuss: https://leetcode.com/problems/shift-distance-between-two-strings/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn shift_distance(s: String, t: String, next_cost: Vec<i32>, previous_cost: Vec<i32>) -> i64 {
53        
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_3361() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.