1945. Sum of Digits of String After Convert Easy

@problem@discussion
#String#Simulation



1/**
2 * [1945] Sum of Digits of String After Convert
3 *
4 * You are given a string s consisting of lowercase English letters, and an integer k.
5 * First, convert s into an integer by replacing each letter with its position in the alphabet (i.e., replace 'a' with 1, 'b' with 2, ..., 'z' with 26). Then, transform the integer by replacing it with the sum of its digits. Repeat the transform operation k times in total.
6 * For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by the following operations:
7 * 
8 * 	Convert: "zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124
9 * 	Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
10 * 	Transform #2: 17 ➝ 1 + 7 ➝ 8
11 * 
12 * Return the resulting integer after performing the operations described above.
13 *  
14 * Example 1:
15 * 
16 * Input: s = "iiii", k = 1
17 * Output: 36
18 * Explanation: The operations are as follows:
19 * - Convert: "iiii" ➝ "(9)(9)(9)(9)" ➝ "9999" ➝ 9999
20 * - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36
21 * Thus the resulting integer is 36.
22 * 
23 * Example 2:
24 * 
25 * Input: s = "leetcode", k = 2
26 * Output: 6
27 * Explanation: The operations are as follows:
28 * - Convert: "leetcode" ➝ "(12)(5)(5)(20)(3)(15)(4)(5)" ➝ "12552031545" ➝ 12552031545
29 * - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33
30 * - Transform #2: 33 ➝ 3 + 3 ➝ 6
31 * Thus the resulting integer is 6.
32 * 
33 * Example 3:
34 * 
35 * Input: s = "zbax", k = 2
36 * Output: 8
37 * 
38 *  
39 * Constraints:
40 * 
41 * 	1 <= s.length <= 100
42 * 	1 <= k <= 10
43 * 	s consists of lowercase English letters.
44 * 
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/sum-of-digits-of-string-after-convert/
49// discuss: https://leetcode.com/problems/sum-of-digits-of-string-after-convert/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54    pub fn get_lucky(s: String, k: i32) -> i32 {
55        0
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_1945() {
67    }
68}
69


Back
© 2025 bowen.ge All Rights Reserved.