diff --git a/solution/1400-1499/1417.Reformat The String/README.md b/solution/1400-1499/1417.Reformat The String/README.md index e63c0337bab68..bcdfb52f1c303 100644 --- a/solution/1400-1499/1417.Reformat The String/README.md +++ b/solution/1400-1499/1417.Reformat The String/README.md @@ -207,6 +207,80 @@ func reformat(s string) string { } ``` +#### TypeScript + +```ts +function reformat(s: string): string { + let a: string[] = []; + let b: string[] = []; + + for (const c of s) { + if (c >= 'a' && c <= 'z') a.push(c); + else if (c >= '0' && c <= '9') b.push(c); + } + + if (Math.abs(a.length - b.length) > 1) { + return ''; + } + + if (a.length < b.length) { + [a, b] = [b, a]; + } + + const ans: string[] = []; + + for (let i = 0; i < b.length; i++) { + ans.push(a[i] + b[i]); + } + + if (a.length > b.length) { + ans.push(a[a.length - 1]); + } + + return ans.join(''); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn reformat(s: String) -> String { + let mut a: Vec = Vec::new(); + let mut b: Vec = Vec::new(); + + for c in s.chars() { + if c.is_ascii_lowercase() { + a.push(c); + } else if c.is_ascii_digit() { + b.push(c); + } + } + + if (a.len() as i32 - b.len() as i32).abs() > 1 { + return String::new(); + } + + if a.len() < b.len() { + std::mem::swap(&mut a, &mut b); + } + + let mut ans = String::new(); + + for i in 0..b.len() { + ans.push(a[i]); + ans.push(b[i]); + } + + if a.len() > b.len() { + ans.push(a[a.len() - 1]); + } + + ans + } +} +``` + diff --git a/solution/1400-1499/1417.Reformat The String/README_EN.md b/solution/1400-1499/1417.Reformat The String/README_EN.md index bd9c918e25d83..4eb70dffca8c5 100644 --- a/solution/1400-1499/1417.Reformat The String/README_EN.md +++ b/solution/1400-1499/1417.Reformat The String/README_EN.md @@ -63,7 +63,15 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We classify all characters in string $s$ into two categories: "digits" and "letters", and put them into arrays $a$ and $b$ respectively. + +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. + +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. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of string $s$. @@ -188,6 +196,80 @@ func reformat(s string) string { } ``` +#### TypeScript + +```ts +function reformat(s: string): string { + let a: string[] = []; + let b: string[] = []; + + for (const c of s) { + if (c >= 'a' && c <= 'z') a.push(c); + else if (c >= '0' && c <= '9') b.push(c); + } + + if (Math.abs(a.length - b.length) > 1) { + return ''; + } + + if (a.length < b.length) { + [a, b] = [b, a]; + } + + const ans: string[] = []; + + for (let i = 0; i < b.length; i++) { + ans.push(a[i] + b[i]); + } + + if (a.length > b.length) { + ans.push(a[a.length - 1]); + } + + return ans.join(''); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn reformat(s: String) -> String { + let mut a: Vec = Vec::new(); + let mut b: Vec = Vec::new(); + + for c in s.chars() { + if c.is_ascii_lowercase() { + a.push(c); + } else if c.is_ascii_digit() { + b.push(c); + } + } + + if (a.len() as i32 - b.len() as i32).abs() > 1 { + return String::new(); + } + + if a.len() < b.len() { + std::mem::swap(&mut a, &mut b); + } + + let mut ans = String::new(); + + for i in 0..b.len() { + ans.push(a[i]); + ans.push(b[i]); + } + + if a.len() > b.len() { + ans.push(a[a.len() - 1]); + } + + ans + } +} +``` + diff --git a/solution/1400-1499/1417.Reformat The String/Solution.rs b/solution/1400-1499/1417.Reformat The String/Solution.rs new file mode 100644 index 0000000000000..9774094e7155b --- /dev/null +++ b/solution/1400-1499/1417.Reformat The String/Solution.rs @@ -0,0 +1,35 @@ +impl Solution { + pub fn reformat(s: String) -> String { + let mut a: Vec = Vec::new(); + let mut b: Vec = Vec::new(); + + for c in s.chars() { + if c.is_ascii_lowercase() { + a.push(c); + } else if c.is_ascii_digit() { + b.push(c); + } + } + + if (a.len() as i32 - b.len() as i32).abs() > 1 { + return String::new(); + } + + if a.len() < b.len() { + std::mem::swap(&mut a, &mut b); + } + + let mut ans = String::new(); + + for i in 0..b.len() { + ans.push(a[i]); + ans.push(b[i]); + } + + if a.len() > b.len() { + ans.push(a[a.len() - 1]); + } + + ans + } +} diff --git a/solution/1400-1499/1417.Reformat The String/Solution.ts b/solution/1400-1499/1417.Reformat The String/Solution.ts new file mode 100644 index 0000000000000..d43c65abae3d9 --- /dev/null +++ b/solution/1400-1499/1417.Reformat The String/Solution.ts @@ -0,0 +1,29 @@ +function reformat(s: string): string { + let a: string[] = []; + let b: string[] = []; + + for (const c of s) { + if (c >= 'a' && c <= 'z') a.push(c); + else if (c >= '0' && c <= '9') b.push(c); + } + + if (Math.abs(a.length - b.length) > 1) { + return ''; + } + + if (a.length < b.length) { + [a, b] = [b, a]; + } + + const ans: string[] = []; + + for (let i = 0; i < b.length; i++) { + ans.push(a[i] + b[i]); + } + + if (a.length > b.length) { + ans.push(a[a.length - 1]); + } + + return ans.join(''); +}