1540. Can Convert String in K Moves Medium

@problem@discussion
#Hash Table#String



1/**
2 * [1540] Can Convert String in K Moves
3 *
4 * Given two strings s and t, your goal is to convert s into t in k moves or less.
5 * During the i^th (<font face="monospace">1 <= i <= k) </font>move you can:
6 * 
7 * 	Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times.
8 * 	Do nothing.
9 * 
10 * Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times.
11 * Remember that any index j can be picked at most once.
12 * Return true if it's possible to convert s into t in no more than k moves, otherwise return false.
13 *  
14 * Example 1:
15 * 
16 * Input: s = "input", t = "ouput", k = 9
17 * Output: true
18 * Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'.
19 * 
20 * Example 2:
21 * 
22 * Input: s = "abc", t = "bcd", k = 10
23 * Output: false
24 * Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s.
25 * 
26 * Example 3:
27 * 
28 * Input: s = "aab", t = "bbb", k = 27
29 * Output: true
30 * Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= s.length, t.length <= 10^5
36 * 	0 <= k <= 10^9
37 * 	s, t contain only lowercase English letters.
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/can-convert-string-in-k-moves/
43// discuss: https://leetcode.com/problems/can-convert-string-in-k-moves/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn can_convert_string(s: String, t: String, k: i32) -> bool {
49        false
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1540() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.