Skip to content

Commit b13ed85

Browse files
authored
feat: add rust solutions for lc No.2839,2840 (#5111)
1 parent e32f055 commit b13ed85

11 files changed

Lines changed: 121 additions & 35 deletions

File tree

solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ tags:
8282
```python
8383
class Solution:
8484
def canBeEqual(self, s1: str, s2: str) -> bool:
85-
return sorted(s1[::2]) == sorted(s2[::2]) and sorted(s1[1::2]) == sorted(
85+
return Counter(s1[::2]) == Counter(s2[::2]) and Counter(s1[1::2]) == Counter(
8686
s2[1::2]
8787
)
8888
```
@@ -155,12 +155,28 @@ function canBeEqual(s1: string, s2: string): boolean {
155155
++cnt[i & 1][s1.charCodeAt(i) - 97];
156156
--cnt[i & 1][s2.charCodeAt(i) - 97];
157157
}
158-
for (let i = 0; i < 26; ++i) {
159-
if (cnt[0][i] || cnt[1][i]) {
160-
return false;
158+
return cnt.every(arr => arr.every(x => x === 0));
159+
}
160+
```
161+
162+
#### Rust
163+
164+
```rust
165+
impl Solution {
166+
pub fn can_be_equal(s1: String, s2: String) -> bool {
167+
let mut cnt: [[i32; 26]; 2] = [[0; 26]; 2];
168+
let n = s1.len();
169+
let s1 = s1.as_bytes();
170+
let s2 = s2.as_bytes();
171+
172+
for i in 0..n {
173+
let idx = (i & 1) as usize;
174+
cnt[idx][(s1[i] - b'a') as usize] += 1;
175+
cnt[idx][(s2[i] - b'a') as usize] -= 1;
161176
}
177+
178+
cnt.iter().all(|row| row.iter().all(|&x| x == 0))
162179
}
163-
return true;
164180
}
165181
```
166182

solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/README_EN.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Similar problems:
8080
```python
8181
class Solution:
8282
def canBeEqual(self, s1: str, s2: str) -> bool:
83-
return sorted(s1[::2]) == sorted(s2[::2]) and sorted(s1[1::2]) == sorted(
83+
return Counter(s1[::2]) == Counter(s2[::2]) and Counter(s1[1::2]) == Counter(
8484
s2[1::2]
8585
)
8686
```
@@ -153,12 +153,28 @@ function canBeEqual(s1: string, s2: string): boolean {
153153
++cnt[i & 1][s1.charCodeAt(i) - 97];
154154
--cnt[i & 1][s2.charCodeAt(i) - 97];
155155
}
156-
for (let i = 0; i < 26; ++i) {
157-
if (cnt[0][i] || cnt[1][i]) {
158-
return false;
156+
return cnt.every(arr => arr.every(x => x === 0));
157+
}
158+
```
159+
160+
#### Rust
161+
162+
```rust
163+
impl Solution {
164+
pub fn can_be_equal(s1: String, s2: String) -> bool {
165+
let mut cnt: [[i32; 26]; 2] = [[0; 26]; 2];
166+
let n = s1.len();
167+
let s1 = s1.as_bytes();
168+
let s2 = s2.as_bytes();
169+
170+
for i in 0..n {
171+
let idx = (i & 1) as usize;
172+
cnt[idx][(s1[i] - b'a') as usize] += 1;
173+
cnt[idx][(s2[i] - b'a') as usize] -= 1;
159174
}
175+
176+
cnt.iter().all(|row| row.iter().all(|&x| x == 0))
160177
}
161-
return true;
162178
}
163179
```
164180

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution:
22
def canBeEqual(self, s1: str, s2: str) -> bool:
3-
return sorted(s1[::2]) == sorted(s2[::2]) and sorted(s1[1::2]) == sorted(
3+
return Counter(s1[::2]) == Counter(s2[::2]) and Counter(s1[1::2]) == Counter(
44
s2[1::2]
55
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn can_be_equal(s1: String, s2: String) -> bool {
3+
let mut cnt: [[i32; 26]; 2] = [[0; 26]; 2];
4+
let n = s1.len();
5+
let s1 = s1.as_bytes();
6+
let s2 = s2.as_bytes();
7+
8+
for i in 0..n {
9+
let idx = (i & 1) as usize;
10+
cnt[idx][(s1[i] - b'a') as usize] += 1;
11+
cnt[idx][(s2[i] - b'a') as usize] -= 1;
12+
}
13+
14+
cnt.iter().all(|row| row.iter().all(|&x| x == 0))
15+
}
16+
}

solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/Solution.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,5 @@ function canBeEqual(s1: string, s2: string): boolean {
44
++cnt[i & 1][s1.charCodeAt(i) - 97];
55
--cnt[i & 1][s2.charCodeAt(i) - 97];
66
}
7-
for (let i = 0; i < 26; ++i) {
8-
if (cnt[0][i] || cnt[1][i]) {
9-
return false;
10-
}
11-
}
12-
return true;
7+
return cnt.every(arr => arr.every(x => x === 0));
138
}

solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ tags:
9090
```python
9191
class Solution:
9292
def checkStrings(self, s1: str, s2: str) -> bool:
93-
return sorted(s1[::2]) == sorted(s2[::2]) and sorted(s1[1::2]) == sorted(
93+
return Counter(s1[::2]) == Counter(s2[::2]) and Counter(s1[1::2]) == Counter(
9494
s2[1::2]
9595
)
9696
```
@@ -163,12 +163,28 @@ function checkStrings(s1: string, s2: string): boolean {
163163
++cnt[i & 1][s1.charCodeAt(i) - 97];
164164
--cnt[i & 1][s2.charCodeAt(i) - 97];
165165
}
166-
for (let i = 0; i < 26; ++i) {
167-
if (cnt[0][i] || cnt[1][i]) {
168-
return false;
166+
return cnt.every(arr => arr.every(x => x === 0));
167+
}
168+
```
169+
170+
#### Rust
171+
172+
```rust
173+
impl Solution {
174+
pub fn check_strings(s1: String, s2: String) -> bool {
175+
let mut cnt: [[i32; 26]; 2] = [[0; 26]; 2];
176+
let n = s1.len();
177+
let s1 = s1.as_bytes();
178+
let s2 = s2.as_bytes();
179+
180+
for i in 0..n {
181+
let idx = (i & 1) as usize;
182+
cnt[idx][(s1[i] - b'a') as usize] += 1;
183+
cnt[idx][(s2[i] - b'a') as usize] -= 1;
169184
}
185+
186+
cnt.iter().all(|row| row.iter().all(|&x| x == 0))
170187
}
171-
return true;
172188
}
173189
```
174190

solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/README_EN.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Similar problems:
8484
```python
8585
class Solution:
8686
def checkStrings(self, s1: str, s2: str) -> bool:
87-
return sorted(s1[::2]) == sorted(s2[::2]) and sorted(s1[1::2]) == sorted(
87+
return Counter(s1[::2]) == Counter(s2[::2]) and Counter(s1[1::2]) == Counter(
8888
s2[1::2]
8989
)
9090
```
@@ -157,12 +157,28 @@ function checkStrings(s1: string, s2: string): boolean {
157157
++cnt[i & 1][s1.charCodeAt(i) - 97];
158158
--cnt[i & 1][s2.charCodeAt(i) - 97];
159159
}
160-
for (let i = 0; i < 26; ++i) {
161-
if (cnt[0][i] || cnt[1][i]) {
162-
return false;
160+
return cnt.every(arr => arr.every(x => x === 0));
161+
}
162+
```
163+
164+
#### Rust
165+
166+
```rust
167+
impl Solution {
168+
pub fn check_strings(s1: String, s2: String) -> bool {
169+
let mut cnt: [[i32; 26]; 2] = [[0; 26]; 2];
170+
let n = s1.len();
171+
let s1 = s1.as_bytes();
172+
let s2 = s2.as_bytes();
173+
174+
for i in 0..n {
175+
let idx = (i & 1) as usize;
176+
cnt[idx][(s1[i] - b'a') as usize] += 1;
177+
cnt[idx][(s2[i] - b'a') as usize] -= 1;
163178
}
179+
180+
cnt.iter().all(|row| row.iter().all(|&x| x == 0))
164181
}
165-
return true;
166182
}
167183
```
168184

solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ public boolean checkStrings(String s1, String s2) {
1212
}
1313
return true;
1414
}
15-
}
15+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution:
22
def checkStrings(self, s1: str, s2: str) -> bool:
3-
return sorted(s1[::2]) == sorted(s2[::2]) and sorted(s1[1::2]) == sorted(
3+
return Counter(s1[::2]) == Counter(s2[::2]) and Counter(s1[1::2]) == Counter(
44
s2[1::2]
55
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn check_strings(s1: String, s2: String) -> bool {
3+
let mut cnt: [[i32; 26]; 2] = [[0; 26]; 2];
4+
let n = s1.len();
5+
let s1 = s1.as_bytes();
6+
let s2 = s2.as_bytes();
7+
8+
for i in 0..n {
9+
let idx = (i & 1) as usize;
10+
cnt[idx][(s1[i] - b'a') as usize] += 1;
11+
cnt[idx][(s2[i] - b'a') as usize] -= 1;
12+
}
13+
14+
cnt.iter().all(|row| row.iter().all(|&x| x == 0))
15+
}
16+
}

0 commit comments

Comments
 (0)