Skip to content

feat: add solutions to lc problem: No.2243 #4148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions solution/2200-2299/2243.Calculate Digit Sum of a String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ tags:
<strong>输出:</strong>"135"
<strong>解释:</strong>
- 第一轮,将 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 。
&nbsp; 这样,s 在第一轮之后变成 "3" + "4" + "6" + "5" = "3465" 。
- 第二轮,将 s 分成:"346" 和 "5" 。
&nbsp; 接着,计算每一组的数字和:3 + 4 + 6 = 13 、5 = 5 。
&nbsp; 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
&nbsp; 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
现在,s.length &lt;= k ,所以返回 "135" 作为答案。
</pre>

Expand All @@ -53,7 +53,7 @@ tags:
<strong>输出:</strong>"000"
<strong>解释:</strong>
将 "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" 。
</pre>

Expand Down Expand Up @@ -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;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ tags:
<pre>
<strong>Input:</strong> s = &quot;11111222223&quot;, k = 3
<strong>Output:</strong> &quot;135&quot;
<strong>Explanation:</strong>
<strong>Explanation:</strong>
- For the first round, we divide s into groups of size 3: &quot;111&quot;, &quot;112&quot;, &quot;222&quot;, and &quot;23&quot;.
​​​​​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.
&nbsp; So, s becomes &quot;3&quot; + &quot;4&quot; + &quot;6&quot; + &quot;5&quot; = &quot;3465&quot; after the first round.
- For the second round, we divide s into &quot;346&quot; and &quot;5&quot;.
&nbsp; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
&nbsp; So, s becomes &quot;13&quot; + &quot;5&quot; = &quot;135&quot; after second round.
&nbsp; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
&nbsp; So, s becomes &quot;13&quot; + &quot;5&quot; = &quot;135&quot; after second round.
Now, s.length &lt;= k, so we return &quot;135&quot; as the answer.
</pre>

Expand All @@ -52,9 +52,9 @@ Now, s.length &lt;= k, so we return &quot;135&quot; as the answer.
<pre>
<strong>Input:</strong> s = &quot;00000000&quot;, k = 3
<strong>Output:</strong> &quot;000&quot;
<strong>Explanation:</strong>
<strong>Explanation:</strong>
We divide s into &quot;000&quot;, &quot;000&quot;, and &quot;00&quot;.
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 &quot;0&quot; + &quot;0&quot; + &quot;0&quot; = &quot;000&quot;, whose length is equal to k, so we return &quot;000&quot;.
</pre>

Expand All @@ -73,7 +73,11 @@ s becomes &quot;0&quot; + &quot;0&quot; + &quot;0&quot; = &quot;000&quot;, whose

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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;
}