@@ -37,13 +37,13 @@ tags:
37
37
<pre >
38
38
<strong >Input:</strong > s = " ; 11111222223" ; , k = 3
39
39
<strong >Output:</strong > " ; 135" ;
40
- <strong >Explanation:</strong >
40
+ <strong >Explanation:</strong >
41
41
- For the first round, we divide s into groups of size 3: " ; 111" ; , " ; 112" ; , " ; 222" ; , and " ; 23" ; .
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.
43
43
  ; So, s becomes " ; 3" ; + " ; 4" ; + " ; 6" ; + " ; 5" ; = " ; 3465" ; after the first round.
44
44
- For the second round, we divide s into " ; 346" ; and " ; 5" ; .
45
-   ; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
46
-   ; So, s becomes " ; 13" ; + " ; 5" ; = " ; 135" ; after second round.
45
+   ; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
46
+   ; So, s becomes " ; 13" ; + " ; 5" ; = " ; 135" ; after second round.
47
47
Now, s.length < ; = k, so we return " ; 135" ; as the answer.
48
48
</pre >
49
49
@@ -52,9 +52,9 @@ Now, s.length <= k, so we return "135" as the answer.
52
52
<pre >
53
53
<strong >Input:</strong > s = " ; 00000000" ; , k = 3
54
54
<strong >Output:</strong > " ; 000" ;
55
- <strong >Explanation:</strong >
55
+ <strong >Explanation:</strong >
56
56
We divide s into " ; 000" ; , " ; 000" ; , and " ; 00" ; .
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.
58
58
s becomes " ; 0" ; + " ; 0" ; + " ; 0" ; = " ; 000" ; , whose length is equal to k, so we return " ; 000" ; .
59
59
</pre >
60
60
@@ -73,7 +73,11 @@ s becomes "0" + "0" + "0" = "000", whose
73
73
74
74
<!-- solution:start -->
75
75
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$.
77
81
78
82
<!-- tabs:start -->
79
83
@@ -163,19 +167,65 @@ func digitSum(s string, k int) string {
163
167
164
168
``` ts
165
169
function digitSum(s : string , k : number ): string {
166
- let ans = [];
167
170
while (s .length > k ) {
171
+ const t: number [] = [];
168
172
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 );
171
178
}
172
- s = ans .join (' ' );
173
- ans = [];
179
+ s = t .join (' ' );
174
180
}
175
181
return s ;
176
182
}
177
183
```
178
184
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
+
179
229
<!-- tabs: end -->
180
230
181
231
<!-- solution: end -->
0 commit comments