Skip to content

Commit 58e753e

Browse files
committed
feat: add solutions to lc problem: No.2451
No.2451.Odd String Difference
1 parent bfeb5b5 commit 58e753e

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

solution/2400-2499/2451.Odd String Difference/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,51 @@ func oddString(words []string) string {
152152
### **TypeScript**
153153

154154
```ts
155+
function oddString(words: string[]): string {
156+
const n = words[0].length;
157+
const map = new Map<string, [boolean, number]>();
158+
words.forEach((word, i) => {
159+
const diff: number[] = [];
160+
for (let j = 1; j < n; j++) {
161+
diff.push(word[j].charCodeAt(0) - word[j - 1].charCodeAt(0));
162+
}
163+
const k = diff.join();
164+
map.set(k, [!map.has(k), i]);
165+
});
166+
for (const [isOnly, i] of map.values()) {
167+
if (isOnly) {
168+
return words[i];
169+
}
170+
}
171+
return '';
172+
}
173+
```
155174

175+
### **Rust**
176+
177+
```rust
178+
use std::collections::HashMap;
179+
impl Solution {
180+
pub fn odd_string(words: Vec<String>) -> String {
181+
let n = words[0].len();
182+
let mut map: HashMap<String, (bool, usize)> = HashMap::new();
183+
for (i, word) in words.iter().enumerate() {
184+
let mut k = String::new();
185+
for j in 1..n {
186+
k.push_str(&(word.as_bytes()[j] - word.as_bytes()[j - 1]).to_string());
187+
k.push(',');
188+
}
189+
let new_is_only = !map.contains_key(&k);
190+
map.insert(k, (new_is_only, i));
191+
}
192+
for (is_only, i) in map.values() {
193+
if *is_only {
194+
return words[*i].clone();
195+
}
196+
}
197+
String::new()
198+
}
199+
}
156200
```
157201

158202
### **...**

solution/2400-2499/2451.Odd String Difference/README_EN.md

+44
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,51 @@ func oddString(words []string) string {
136136
### **TypeScript**
137137

138138
```ts
139+
function oddString(words: string[]): string {
140+
const n = words[0].length;
141+
const map = new Map<string, [boolean, number]>();
142+
words.forEach((word, i) => {
143+
const diff: number[] = [];
144+
for (let j = 1; j < n; j++) {
145+
diff.push(word[j].charCodeAt(0) - word[j - 1].charCodeAt(0));
146+
}
147+
const k = diff.join();
148+
map.set(k, [!map.has(k), i]);
149+
});
150+
for (const [isOnly, i] of map.values()) {
151+
if (isOnly) {
152+
return words[i];
153+
}
154+
}
155+
return '';
156+
}
157+
```
139158

159+
### **Rust**
160+
161+
```rust
162+
use std::collections::HashMap;
163+
impl Solution {
164+
pub fn odd_string(words: Vec<String>) -> String {
165+
let n = words[0].len();
166+
let mut map: HashMap<String, (bool, usize)> = HashMap::new();
167+
for (i, word) in words.iter().enumerate() {
168+
let mut k = String::new();
169+
for j in 1..n {
170+
k.push_str(&(word.as_bytes()[j] - word.as_bytes()[j - 1]).to_string());
171+
k.push(',');
172+
}
173+
let new_is_only = !map.contains_key(&k);
174+
map.insert(k, (new_is_only, i));
175+
}
176+
for (is_only, i) in map.values() {
177+
if *is_only {
178+
return words[*i].clone();
179+
}
180+
}
181+
String::new()
182+
}
183+
}
140184
```
141185

142186
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::collections::HashMap;
2+
impl Solution {
3+
pub fn odd_string(words: Vec<String>) -> String {
4+
let n = words[0].len();
5+
let mut map: HashMap<String, (bool, usize)> = HashMap::new();
6+
for (i, word) in words.iter().enumerate() {
7+
let mut k = String::new();
8+
for j in 1..n {
9+
k.push_str(&(word.as_bytes()[j] - word.as_bytes()[j - 1]).to_string());
10+
k.push(',');
11+
}
12+
let new_is_only = !map.contains_key(&k);
13+
map.insert(k, (new_is_only, i));
14+
}
15+
for (is_only, i) in map.values() {
16+
if *is_only {
17+
return words[*i].clone();
18+
}
19+
}
20+
String::new()
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function oddString(words: string[]): string {
2+
const n = words[0].length;
3+
const map = new Map<string, [boolean, number]>();
4+
words.forEach((word, i) => {
5+
const diff: number[] = [];
6+
for (let j = 1; j < n; j++) {
7+
diff.push(word[j].charCodeAt(0) - word[j - 1].charCodeAt(0));
8+
}
9+
const k = diff.join();
10+
map.set(k, [!map.has(k), i]);
11+
});
12+
for (const [isOnly, i] of map.values()) {
13+
if (isOnly) {
14+
return words[i];
15+
}
16+
}
17+
return '';
18+
}

0 commit comments

Comments
 (0)