Skip to content

Commit a69bbc0

Browse files
authored
feat: add solutions to lc problem: No.2243 (#4148)
No.2243.Calculate Digit Sum of a String
1 parent d616e81 commit a69bbc0

File tree

5 files changed

+157
-25
lines changed

5 files changed

+157
-25
lines changed

solution/2200-2299/2243.Calculate Digit Sum of a String/README.md

+54-8
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ tags:
3939
<strong>输出:</strong>"135"
4040
<strong>解释:</strong>
4141
- 第一轮,将 s 分成:"111"、"112"、"222" 和 "23" 。
42-
接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。
42+
接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。
4343
&nbsp; 这样,s 在第一轮之后变成 "3" + "4" + "6" + "5" = "3465" 。
4444
- 第二轮,将 s 分成:"346" 和 "5" 。
4545
&nbsp; 接着,计算每一组的数字和:3 + 4 + 6 = 13 、5 = 5 。
46-
&nbsp; 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
46+
&nbsp; 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
4747
现在,s.length &lt;= k ,所以返回 "135" 作为答案。
4848
</pre>
4949

@@ -53,7 +53,7 @@ tags:
5353
<strong>输出:</strong>"000"
5454
<strong>解释:</strong>
5555
将 "000", "000", and "00".
56-
接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。
56+
接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。
5757
s 变为 "0" + "0" + "0" = "000" ,其长度等于 k ,所以返回 "000" 。
5858
</pre>
5959

@@ -167,19 +167,65 @@ func digitSum(s string, k int) string {
167167

168168
```ts
169169
function digitSum(s: string, k: number): string {
170-
let ans = [];
171170
while (s.length > k) {
171+
const t: number[] = [];
172172
for (let i = 0; i < s.length; i += k) {
173-
let cur = s.slice(i, i + k);
174-
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
173+
const x = s
174+
.slice(i, i + k)
175+
.split('')
176+
.reduce((a, b) => a + +b, 0);
177+
t.push(x);
175178
}
176-
s = ans.join('');
177-
ans = [];
179+
s = t.join('');
178180
}
179181
return s;
180182
}
181183
```
182184

185+
#### Rust
186+
187+
```rust
188+
impl Solution {
189+
pub fn digit_sum(s: String, k: i32) -> String {
190+
let mut s = s;
191+
let k = k as usize;
192+
while s.len() > k {
193+
let mut t = Vec::new();
194+
for chunk in s.as_bytes().chunks(k) {
195+
let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum();
196+
t.push(sum.to_string());
197+
}
198+
s = t.join("");
199+
}
200+
s
201+
}
202+
}
203+
```
204+
205+
#### JavaScript
206+
207+
```js
208+
/**
209+
* @param {string} s
210+
* @param {number} k
211+
* @return {string}
212+
*/
213+
var digitSum = function (s, k) {
214+
while (s.length > k) {
215+
const t = [];
216+
for (let i = 0; i < s.length; i += k) {
217+
const x = s
218+
.slice(i, i + k)
219+
.split('')
220+
.reduce((a, b) => a + +b, 0);
221+
t.push(x);
222+
}
223+
s = t.join('');
224+
}
225+
return s;
226+
};
227+
```
228+
183229
<!-- tabs:end -->
184230

185231
<!-- solution:end -->

solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md

+62-12
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> s = &quot;11111222223&quot;, k = 3
3939
<strong>Output:</strong> &quot;135&quot;
40-
<strong>Explanation:</strong>
40+
<strong>Explanation:</strong>
4141
- 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;.
42-
​​​​​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.
42+
​​​​​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.
4343
&nbsp; So, s becomes &quot;3&quot; + &quot;4&quot; + &quot;6&quot; + &quot;5&quot; = &quot;3465&quot; after the first round.
4444
- For the second round, we divide s into &quot;346&quot; and &quot;5&quot;.
45-
&nbsp; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
46-
&nbsp; So, s becomes &quot;13&quot; + &quot;5&quot; = &quot;135&quot; after second round.
45+
&nbsp; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
46+
&nbsp; So, s becomes &quot;13&quot; + &quot;5&quot; = &quot;135&quot; after second round.
4747
Now, s.length &lt;= k, so we return &quot;135&quot; as the answer.
4848
</pre>
4949

@@ -52,9 +52,9 @@ Now, s.length &lt;= k, so we return &quot;135&quot; as the answer.
5252
<pre>
5353
<strong>Input:</strong> s = &quot;00000000&quot;, k = 3
5454
<strong>Output:</strong> &quot;000&quot;
55-
<strong>Explanation:</strong>
55+
<strong>Explanation:</strong>
5656
We divide s into &quot;000&quot;, &quot;000&quot;, and &quot;00&quot;.
57-
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
57+
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
5858
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;.
5959
</pre>
6060

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

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Simulation
77+
78+
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.
79+
80+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
7781

7882
<!-- tabs:start -->
7983

@@ -163,19 +167,65 @@ func digitSum(s string, k int) string {
163167

164168
```ts
165169
function digitSum(s: string, k: number): string {
166-
let ans = [];
167170
while (s.length > k) {
171+
const t: number[] = [];
168172
for (let i = 0; i < s.length; i += k) {
169-
let cur = s.slice(i, i + k);
170-
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
173+
const x = s
174+
.slice(i, i + k)
175+
.split('')
176+
.reduce((a, b) => a + +b, 0);
177+
t.push(x);
171178
}
172-
s = ans.join('');
173-
ans = [];
179+
s = t.join('');
174180
}
175181
return s;
176182
}
177183
```
178184

185+
#### Rust
186+
187+
```rust
188+
impl Solution {
189+
pub fn digit_sum(s: String, k: i32) -> String {
190+
let mut s = s;
191+
let k = k as usize;
192+
while s.len() > k {
193+
let mut t = Vec::new();
194+
for chunk in s.as_bytes().chunks(k) {
195+
let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum();
196+
t.push(sum.to_string());
197+
}
198+
s = t.join("");
199+
}
200+
s
201+
}
202+
}
203+
```
204+
205+
#### JavaScript
206+
207+
```js
208+
/**
209+
* @param {string} s
210+
* @param {number} k
211+
* @return {string}
212+
*/
213+
var digitSum = function (s, k) {
214+
while (s.length > k) {
215+
const t = [];
216+
for (let i = 0; i < s.length; i += k) {
217+
const x = s
218+
.slice(i, i + k)
219+
.split('')
220+
.reduce((a, b) => a + +b, 0);
221+
t.push(x);
222+
}
223+
s = t.join('');
224+
}
225+
return s;
226+
};
227+
```
228+
179229
<!-- tabs:end -->
180230

181231
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {string}
5+
*/
6+
var digitSum = function (s, k) {
7+
while (s.length > k) {
8+
const t = [];
9+
for (let i = 0; i < s.length; i += k) {
10+
const x = s
11+
.slice(i, i + k)
12+
.split('')
13+
.reduce((a, b) => a + +b, 0);
14+
t.push(x);
15+
}
16+
s = t.join('');
17+
}
18+
return s;
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn digit_sum(s: String, k: i32) -> String {
3+
let mut s = s;
4+
let k = k as usize;
5+
while s.len() > k {
6+
let mut t = Vec::new();
7+
for chunk in s.as_bytes().chunks(k) {
8+
let sum: i32 = chunk.iter().map(|&c| (c - b'0') as i32).sum();
9+
t.push(sum.to_string());
10+
}
11+
s = t.join("");
12+
}
13+
s
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
function digitSum(s: string, k: number): string {
2-
let ans = [];
32
while (s.length > k) {
3+
const t: number[] = [];
44
for (let i = 0; i < s.length; i += k) {
5-
let cur = s.slice(i, i + k);
6-
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
5+
const x = s
6+
.slice(i, i + k)
7+
.split('')
8+
.reduce((a, b) => a + +b, 0);
9+
t.push(x);
710
}
8-
s = ans.join('');
9-
ans = [];
11+
s = t.join('');
1012
}
1113
return s;
1214
}

0 commit comments

Comments
 (0)