diff --git a/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md b/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md index 18c281783698b..96104c8905d1e 100644 --- a/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md +++ b/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md @@ -39,11 +39,11 @@ tags: 输出:"135" 解释: - 第一轮,将 s 分成:"111"、"112"、"222" 和 "23" 。 - 接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。 + 接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。 这样,s 在第一轮之后变成 "3" + "4" + "6" + "5" = "3465" 。 - 第二轮,将 s 分成:"346" 和 "5" 。 接着,计算每一组的数字和:3 + 4 + 6 = 13 、5 = 5 。 - 这样,s 在第二轮之后变成 "13" + "5" = "135" 。 + 这样,s 在第二轮之后变成 "13" + "5" = "135" 。 现在,s.length <= k ,所以返回 "135" 作为答案。 @@ -53,7 +53,7 @@ tags: 输出:"000" 解释: 将 "000", "000", and "00". -接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。 +接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。 s 变为 "0" + "0" + "0" = "000" ,其长度等于 k ,所以返回 "000" 。 @@ -167,19 +167,65 @@ func digitSum(s string, k int) string { ```ts function digitSum(s: string, k: number): string { - let ans = []; while (s.length > k) { + const t: number[] = []; for (let i = 0; i < s.length; i += k) { - let cur = s.slice(i, i + k); - ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0)); + const x = s + .slice(i, i + k) + .split('') + .reduce((a, b) => a + +b, 0); + t.push(x); } - s = ans.join(''); - ans = []; + s = t.join(''); } return s; } ``` +#### Rust + +```rust +impl Solution { + pub fn digit_sum(s: String, k: i32) -> String { + let mut s = s; + let k = k as usize; + while s.len() > k { + let mut t = Vec::new(); + for chunk in s.as_bytes().chunks(k) { + let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum(); + t.push(sum.to_string()); + } + s = t.join(""); + } + s + } +} +``` + +#### JavaScript + +```js +/** + * @param {string} s + * @param {number} k + * @return {string} + */ +var digitSum = function (s, k) { + while (s.length > k) { + const t = []; + for (let i = 0; i < s.length; i += k) { + const x = s + .slice(i, i + k) + .split('') + .reduce((a, b) => a + +b, 0); + t.push(x); + } + s = t.join(''); + } + return s; +}; +``` + diff --git a/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md b/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md index bc65eccc0d046..c423adb00dead 100644 --- a/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md +++ b/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md @@ -37,13 +37,13 @@ tags:
Input: s = "11111222223", k = 3 Output: "135" -Explanation: +Explanation: - For the first round, we divide s into groups of size 3: "111", "112", "222", and "23". - 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. + 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. So, s becomes "3" + "4" + "6" + "5" = "3465" after the first round. - For the second round, we divide s into "346" and "5". - Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5. - So, s becomes "13" + "5" = "135" after second round. + Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5. + So, s becomes "13" + "5" = "135" after second round. Now, s.length <= k, so we return "135" as the answer.@@ -52,9 +52,9 @@ Now, s.length <= k, so we return "135" as the answer.
Input: s = "00000000", k = 3 Output: "000" -Explanation: +Explanation: We divide s into "000", "000", and "00". -Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0. +Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0. s becomes "0" + "0" + "0" = "000", whose length is equal to k, so we return "000".@@ -73,7 +73,11 @@ s becomes "0" + "0" + "0" = "000", whose -### Solution 1 +### Solution 1: Simulation + +According to the problem statement, we can simulate the operations described in the problem until the length of the string is less than or equal to $k$. Finally, return the string. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$. @@ -163,19 +167,65 @@ func digitSum(s string, k int) string { ```ts function digitSum(s: string, k: number): string { - let ans = []; while (s.length > k) { + const t: number[] = []; for (let i = 0; i < s.length; i += k) { - let cur = s.slice(i, i + k); - ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0)); + const x = s + .slice(i, i + k) + .split('') + .reduce((a, b) => a + +b, 0); + t.push(x); } - s = ans.join(''); - ans = []; + s = t.join(''); } return s; } ``` +#### Rust + +```rust +impl Solution { + pub fn digit_sum(s: String, k: i32) -> String { + let mut s = s; + let k = k as usize; + while s.len() > k { + let mut t = Vec::new(); + for chunk in s.as_bytes().chunks(k) { + let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum(); + t.push(sum.to_string()); + } + s = t.join(""); + } + s + } +} +``` + +#### JavaScript + +```js +/** + * @param {string} s + * @param {number} k + * @return {string} + */ +var digitSum = function (s, k) { + while (s.length > k) { + const t = []; + for (let i = 0; i < s.length; i += k) { + const x = s + .slice(i, i + k) + .split('') + .reduce((a, b) => a + +b, 0); + t.push(x); + } + s = t.join(''); + } + return s; +}; +``` + diff --git a/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.js b/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.js new file mode 100644 index 0000000000000..6e6452385b171 --- /dev/null +++ b/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.js @@ -0,0 +1,19 @@ +/** + * @param {string} s + * @param {number} k + * @return {string} + */ +var digitSum = function (s, k) { + while (s.length > k) { + const t = []; + for (let i = 0; i < s.length; i += k) { + const x = s + .slice(i, i + k) + .split('') + .reduce((a, b) => a + +b, 0); + t.push(x); + } + s = t.join(''); + } + return s; +}; diff --git a/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.rs b/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.rs new file mode 100644 index 0000000000000..3c387fd5f1f0f --- /dev/null +++ b/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.rs @@ -0,0 +1,15 @@ +impl Solution { + pub fn digit_sum(s: String, k: i32) -> String { + let mut s = s; + let k = k as usize; + while s.len() > k { + let mut t = Vec::new(); + for chunk in s.as_bytes().chunks(k) { + let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum(); + t.push(sum.to_string()); + } + s = t.join(""); + } + s + } +} diff --git a/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.ts b/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.ts index 63cb60cbc6525..171e221a8b519 100644 --- a/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.ts +++ b/solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.ts @@ -1,12 +1,14 @@ function digitSum(s: string, k: number): string { - let ans = []; while (s.length > k) { + const t: number[] = []; for (let i = 0; i < s.length; i += k) { - let cur = s.slice(i, i + k); - ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0)); + const x = s + .slice(i, i + k) + .split('') + .reduce((a, b) => a + +b, 0); + t.push(x); } - s = ans.join(''); - ans = []; + s = t.join(''); } return s; }