Skip to content

Commit c22298e

Browse files
committed
feat: add solutions for lc No.1417
1 parent a4dec26 commit c22298e

4 files changed

Lines changed: 221 additions & 1 deletion

File tree

solution/1400-1499/1417.Reformat The String/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,80 @@ func reformat(s string) string {
207207
}
208208
```
209209

210+
#### TypeScript
211+
212+
```ts
213+
function reformat(s: string): string {
214+
let a: string[] = [];
215+
let b: string[] = [];
216+
217+
for (const c of s) {
218+
if (c >= 'a' && c <= 'z') a.push(c);
219+
else if (c >= '0' && c <= '9') b.push(c);
220+
}
221+
222+
if (Math.abs(a.length - b.length) > 1) {
223+
return '';
224+
}
225+
226+
if (a.length < b.length) {
227+
[a, b] = [b, a];
228+
}
229+
230+
const ans: string[] = [];
231+
232+
for (let i = 0; i < b.length; i++) {
233+
ans.push(a[i] + b[i]);
234+
}
235+
236+
if (a.length > b.length) {
237+
ans.push(a[a.length - 1]);
238+
}
239+
240+
return ans.join('');
241+
}
242+
```
243+
244+
#### Rust
245+
246+
```rust
247+
impl Solution {
248+
pub fn reformat(s: String) -> String {
249+
let mut a: Vec<char> = Vec::new();
250+
let mut b: Vec<char> = Vec::new();
251+
252+
for c in s.chars() {
253+
if c.is_ascii_lowercase() {
254+
a.push(c);
255+
} else if c.is_ascii_digit() {
256+
b.push(c);
257+
}
258+
}
259+
260+
if (a.len() as i32 - b.len() as i32).abs() > 1 {
261+
return String::new();
262+
}
263+
264+
if a.len() < b.len() {
265+
std::mem::swap(&mut a, &mut b);
266+
}
267+
268+
let mut ans = String::new();
269+
270+
for i in 0..b.len() {
271+
ans.push(a[i]);
272+
ans.push(b[i]);
273+
}
274+
275+
if a.len() > b.len() {
276+
ans.push(a[a.len() - 1]);
277+
}
278+
279+
ans
280+
}
281+
}
282+
```
283+
210284
<!-- tabs:end -->
211285

212286
<!-- solution:end -->

solution/1400-1499/1417.Reformat The String/README_EN.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Simulation
67+
68+
We classify all characters in string $s$ into two categories: "digits" and "letters", and put them into arrays $a$ and $b$ respectively.
69+
70+
Compare the lengths of $a$ and $b$. If the length of $a$ is less than $b$, swap $a$ and $b$. Then check the difference in lengths; if it exceeds $1$, return an empty string.
71+
72+
Next, iterate through both arrays simultaneously, appending characters from $a$ and $b$ alternately to the answer. After the iteration, if $a$ is longer than $b$, append the last character of $a$ to the answer.
73+
74+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of string $s$.
6775

6876
<!-- tabs:start -->
6977

@@ -188,6 +196,80 @@ func reformat(s string) string {
188196
}
189197
```
190198

199+
#### TypeScript
200+
201+
```ts
202+
function reformat(s: string): string {
203+
let a: string[] = [];
204+
let b: string[] = [];
205+
206+
for (const c of s) {
207+
if (c >= 'a' && c <= 'z') a.push(c);
208+
else if (c >= '0' && c <= '9') b.push(c);
209+
}
210+
211+
if (Math.abs(a.length - b.length) > 1) {
212+
return '';
213+
}
214+
215+
if (a.length < b.length) {
216+
[a, b] = [b, a];
217+
}
218+
219+
const ans: string[] = [];
220+
221+
for (let i = 0; i < b.length; i++) {
222+
ans.push(a[i] + b[i]);
223+
}
224+
225+
if (a.length > b.length) {
226+
ans.push(a[a.length - 1]);
227+
}
228+
229+
return ans.join('');
230+
}
231+
```
232+
233+
#### Rust
234+
235+
```rust
236+
impl Solution {
237+
pub fn reformat(s: String) -> String {
238+
let mut a: Vec<char> = Vec::new();
239+
let mut b: Vec<char> = Vec::new();
240+
241+
for c in s.chars() {
242+
if c.is_ascii_lowercase() {
243+
a.push(c);
244+
} else if c.is_ascii_digit() {
245+
b.push(c);
246+
}
247+
}
248+
249+
if (a.len() as i32 - b.len() as i32).abs() > 1 {
250+
return String::new();
251+
}
252+
253+
if a.len() < b.len() {
254+
std::mem::swap(&mut a, &mut b);
255+
}
256+
257+
let mut ans = String::new();
258+
259+
for i in 0..b.len() {
260+
ans.push(a[i]);
261+
ans.push(b[i]);
262+
}
263+
264+
if a.len() > b.len() {
265+
ans.push(a[a.len() - 1]);
266+
}
267+
268+
ans
269+
}
270+
}
271+
```
272+
191273
<!-- tabs:end -->
192274

193275
<!-- solution:end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn reformat(s: String) -> String {
3+
let mut a: Vec<char> = Vec::new();
4+
let mut b: Vec<char> = Vec::new();
5+
6+
for c in s.chars() {
7+
if c.is_ascii_lowercase() {
8+
a.push(c);
9+
} else if c.is_ascii_digit() {
10+
b.push(c);
11+
}
12+
}
13+
14+
if (a.len() as i32 - b.len() as i32).abs() > 1 {
15+
return String::new();
16+
}
17+
18+
if a.len() < b.len() {
19+
std::mem::swap(&mut a, &mut b);
20+
}
21+
22+
let mut ans = String::new();
23+
24+
for i in 0..b.len() {
25+
ans.push(a[i]);
26+
ans.push(b[i]);
27+
}
28+
29+
if a.len() > b.len() {
30+
ans.push(a[a.len() - 1]);
31+
}
32+
33+
ans
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function reformat(s: string): string {
2+
let a: string[] = [];
3+
let b: string[] = [];
4+
5+
for (const c of s) {
6+
if (c >= 'a' && c <= 'z') a.push(c);
7+
else if (c >= '0' && c <= '9') b.push(c);
8+
}
9+
10+
if (Math.abs(a.length - b.length) > 1) {
11+
return '';
12+
}
13+
14+
if (a.length < b.length) {
15+
[a, b] = [b, a];
16+
}
17+
18+
const ans: string[] = [];
19+
20+
for (let i = 0; i < b.length; i++) {
21+
ans.push(a[i] + b[i]);
22+
}
23+
24+
if (a.length > b.length) {
25+
ans.push(a[a.length - 1]);
26+
}
27+
28+
return ans.join('');
29+
}

0 commit comments

Comments
 (0)