2243. Calculate Digit Sum of a String Easy

@problem@discussion
#String#Simulation



1/**
2 * [2243] Calculate Digit Sum of a String
3 *
4 * You are given a string s consisting of digits and an integer k.
5 * A round can be completed if the length of s is greater than k. In one round, do the following:
6 * <ol>
7 * Divide s into consecutive groups of size k such that the first k characters are in the first group, the next k characters are in the second group, and so on. Note that the size of the last group can be smaller than k.
8 * Replace each group of s with a string representing the sum of all its digits. For example, "346" is replaced with "13" because 3 + 4 + 6 = 13.
9 * Merge consecutive groups together to form a new string. If the length of the string is greater than k, repeat from step 1.
10 * </ol>
11 * Return s after all rounds have been completed.
12 *  
13 * Example 1:
14 *
15 * Input: s = "11111222223", k = 3
16 * Output: "135"
17 * Explanation:
18 * - For the first round, we divide s into groups of size 3: "111", "112", "222", and "23".
19 *   ​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
20 *   So, s becomes "3" + "4" + "6" + "5" = "3465" after the first round.
21 * - For the second round, we divide s into "346" and "5".
22 *   Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
23 *   So, s becomes "13" + "5" = "135" after second round.
24 * Now, s.length <= k, so we return "135" as the answer.
25 *
26 * Example 2:
27 *
28 * Input: s = "00000000", k = 3
29 * Output: "000"
30 * Explanation:
31 * We divide s into "000", "000", and "00".
32 * Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
33 * s becomes "0" + "0" + "0" = "000", whose length is equal to k, so we return "000".
34 *
35 *  
36 * Constraints:
37 *
38 * 1 <= s.length <= 100
39 * 2 <= k <= 100
40 * s consists of digits only.
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/calculate-digit-sum-of-a-string/
46// discuss: https://leetcode.com/problems/calculate-digit-sum-of-a-string/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn digit_sum(s: String, k: i32) -> String {
52        if s.len() <= k as usize {
53            return s;
54        }
55
56        let mut i = 0;
57        let mut result = String::new();
58
59        while i < s.len() {
60            let part = &s[i..if i + k as usize > s.len() {
61                s.len()
62            } else {
63                i + k as usize
64            }];
65            let converted = Self::convert(part);
66            result.push_str(&converted);
67            i += k as usize;
68        }
69
70        if result.len() <= k as usize {
71            result
72        } else {
73            Self::digit_sum(result, k)
74        }
75    }
76
77    fn convert(s: &str) -> String {
78        s.chars()
79            .map(|c| c as i32 - '0' as i32)
80            .sum::<i32>()
81            .to_string()
82    }
83}
84
85// submission codes end
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn test_2243() {
93        assert_eq!(
94            Solution::digit_sum("11111222223".to_string(), 3),
95            "135".to_owned()
96        );
97        assert_eq!(
98            Solution::digit_sum("00000000".to_string(), 3),
99            "000".to_owned()
100        );
101        assert_eq!(
102            Solution::digit_sum("12".to_string(), 3),
103            "12".to_owned()
104        );
105    }
106}
107


Back
© 2025 bowen.ge All Rights Reserved.