|
| 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. Your task is to convert the string into an integer by a special process, and then transform it by summing its digits repeatedly k times. More specifically, perform the following steps: |
| 5 | + * <ol> |
| 6 | + * 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). |
| 7 | + * Transform the integer by replacing it with the sum of its digits. |
| 8 | + * Repeat the transform operation (step 2) k times in total. |
| 9 | + * </ol> |
| 10 | + * For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by the following operations: |
| 11 | + * <ol> |
| 12 | + * Convert: "zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124 |
| 13 | + * Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17 |
| 14 | + * Transform #2: 17 ➝ 1 + 7 ➝ 8 |
| 15 | + * </ol> |
| 16 | + * Return the resulting integer after performing the operations described above. |
| 17 | + * |
| 18 | + * Example 1: |
| 19 | + * <div class="example-block"> |
| 20 | + * Input: <span class="example-io">s = "iiii", k = 1</span> |
| 21 | + * Output: <span class="example-io">36</span> |
| 22 | + * Explanation: |
| 23 | + * The operations are as follows:<br /> |
| 24 | + * - Convert: "iiii" ➝ "(9)(9)(9)(9)" ➝ "9999" ➝ 9999<br /> |
| 25 | + * - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36<br /> |
| 26 | + * Thus the resulting integer is 36. |
| 27 | + * </div> |
| 28 | + * Example 2: |
| 29 | + * <div class="example-block"> |
| 30 | + * Input: <span class="example-io">s = "leetcode", k = 2</span> |
| 31 | + * Output: <span class="example-io">6</span> |
| 32 | + * Explanation: |
| 33 | + * The operations are as follows:<br /> |
| 34 | + * - Convert: "leetcode" ➝ "(12)(5)(5)(20)(3)(15)(4)(5)" ➝ "12552031545" ➝ 12552031545<br /> |
| 35 | + * - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33<br /> |
| 36 | + * - Transform #2: 33 ➝ 3 + 3 ➝ 6<br /> |
| 37 | + * Thus the resulting integer is 6. |
| 38 | + * </div> |
| 39 | + * Example 3: |
| 40 | + * <div class="example-block"> |
| 41 | + * Input: <span class="example-io">s = "zbax", k = 2</span> |
| 42 | + * Output: <span class="example-io">8</span> |
| 43 | + * </div> |
| 44 | + * |
| 45 | + * Constraints: |
| 46 | + * |
| 47 | + * 1 <= s.length <= 100 |
| 48 | + * 1 <= k <= 10 |
| 49 | + * s consists of lowercase English letters. |
| 50 | + * |
| 51 | + */ |
| 52 | +pub struct Solution {} |
| 53 | + |
| 54 | +// problem: https://leetcode.com/problems/sum-of-digits-of-string-after-convert/ |
| 55 | +// discuss: https://leetcode.com/problems/sum-of-digits-of-string-after-convert/discuss/?currentPage=1&orderBy=most_votes&query= |
| 56 | + |
| 57 | +// submission codes start here |
| 58 | + |
| 59 | +impl Solution { |
| 60 | + pub fn get_lucky(s: String, k: i32) -> i32 { |
| 61 | + let mut s = s |
| 62 | + .chars() |
| 63 | + .map(|c| (c as u8 - b'a' + 1).to_string()) |
| 64 | + .collect::<Vec<String>>() |
| 65 | + .join(""); |
| 66 | + |
| 67 | + for _ in 0..k { |
| 68 | + s = s |
| 69 | + .chars() |
| 70 | + .map(|c| c as i32 - '0' as i32) |
| 71 | + .sum::<i32>() |
| 72 | + .to_string(); |
| 73 | + } |
| 74 | + |
| 75 | + s.parse::<i32>().unwrap() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// submission codes end |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_1945_example_1() { |
| 87 | + let s = "iiii".to_string(); |
| 88 | + let k = 1; |
| 89 | + |
| 90 | + let result = 36; |
| 91 | + |
| 92 | + assert_eq!(Solution::get_lucky(s, k), result); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_1945_example_2() { |
| 97 | + let s = "leetcode".to_string(); |
| 98 | + let k = 2; |
| 99 | + |
| 100 | + let result = 6; |
| 101 | + |
| 102 | + assert_eq!(Solution::get_lucky(s, k), result); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_1945_example_3() { |
| 107 | + let s = "zbax".to_string(); |
| 108 | + let k = 2; |
| 109 | + |
| 110 | + let result = 8; |
| 111 | + |
| 112 | + assert_eq!(Solution::get_lucky(s, k), result); |
| 113 | + } |
| 114 | +} |
0 commit comments