Skip to content

Commit 7e2ec68

Browse files
committed
feat: add solutions to lc problem: No.1704
No.1704.Determine if String Halves Are Alike
1 parent 705a881 commit 7e2ec68

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

solution/1700-1799/1704.Determine if String Halves Are Alike/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,46 @@ func halvesAreAlike(s string) bool {
133133
}
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
function halvesAreAlike(s: string): boolean {
140+
const set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
141+
const n = s.length >> 1;
142+
let count = 0;
143+
for (let i = 0; i < n; i++) {
144+
set.has(s[i]) && count++;
145+
set.has(s[n + i]) && count--;
146+
}
147+
return count === 0;
148+
}
149+
```
150+
151+
### **Rust**
152+
153+
```rust
154+
use std::collections::HashSet;
155+
impl Solution {
156+
pub fn halves_are_alike(s: String) -> bool {
157+
let set: HashSet<&u8> = [b'a', b'e', b'i', b'o', b'u', b'A', b'E', b'I', b'O', b'U']
158+
.into_iter()
159+
.collect();
160+
let s = s.as_bytes();
161+
let n = s.len() >> 1;
162+
let mut count = 0;
163+
for i in 0..n {
164+
if set.contains(&s[i]) {
165+
count += 1;
166+
}
167+
if set.contains(&s[n + i]) {
168+
count -= 1;
169+
}
170+
}
171+
count == 0
172+
}
173+
}
174+
```
175+
136176
### **...**
137177

138178
```

solution/1700-1799/1704.Determine if String Halves Are Alike/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,46 @@ func halvesAreAlike(s string) bool {
117117
}
118118
```
119119

120+
### **TypeScript**
121+
122+
```ts
123+
function halvesAreAlike(s: string): boolean {
124+
const set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
125+
const n = s.length >> 1;
126+
let count = 0;
127+
for (let i = 0; i < n; i++) {
128+
set.has(s[i]) && count++;
129+
set.has(s[n + i]) && count--;
130+
}
131+
return count === 0;
132+
}
133+
```
134+
135+
### **Rust**
136+
137+
```rust
138+
use std::collections::HashSet;
139+
impl Solution {
140+
pub fn halves_are_alike(s: String) -> bool {
141+
let set: HashSet<&u8> = [b'a', b'e', b'i', b'o', b'u', b'A', b'E', b'I', b'O', b'U']
142+
.into_iter()
143+
.collect();
144+
let s = s.as_bytes();
145+
let n = s.len() >> 1;
146+
let mut count = 0;
147+
for i in 0..n {
148+
if set.contains(&s[i]) {
149+
count += 1;
150+
}
151+
if set.contains(&s[n + i]) {
152+
count -= 1;
153+
}
154+
}
155+
count == 0
156+
}
157+
}
158+
```
159+
120160
### **...**
121161

122162
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn halves_are_alike(s: String) -> bool {
4+
let set: HashSet<&u8> = [b'a', b'e', b'i', b'o', b'u', b'A', b'E', b'I', b'O', b'U']
5+
.into_iter()
6+
.collect();
7+
let s = s.as_bytes();
8+
let n = s.len() >> 1;
9+
let mut count = 0;
10+
for i in 0..n {
11+
if set.contains(&s[i]) {
12+
count += 1;
13+
}
14+
if set.contains(&s[n + i]) {
15+
count -= 1;
16+
}
17+
}
18+
count == 0
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function halvesAreAlike(s: string): boolean {
2+
const set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
3+
const n = s.length >> 1;
4+
let count = 0;
5+
for (let i = 0; i < n; i++) {
6+
set.has(s[i]) && count++;
7+
set.has(s[n + i]) && count--;
8+
}
9+
return count === 0;
10+
}

0 commit comments

Comments
 (0)