Skip to content

Commit ff3ab0d

Browse files
authored
feat: add rust solution to lc problem: No.0767 (doocs#1501)
1 parent 4109feb commit ff3ab0d

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

solution/0700-0799/0767.Reorganize String/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,54 @@ public:
260260
};
261261
```
262262

263+
### **Rust**
264+
265+
```rust
266+
use std::collections::{HashMap, BinaryHeap, VecDeque};
267+
268+
impl Solution {
269+
#[allow(dead_code)]
270+
pub fn reorganize_string(s: String) -> String {
271+
let mut map = HashMap::new();
272+
let mut pq = BinaryHeap::new();
273+
let mut ret = String::new();
274+
let mut queue = VecDeque::new();
275+
let n = s.len();
276+
277+
// Initialize the HashMap
278+
for c in s.chars() {
279+
map
280+
.entry(c)
281+
.and_modify(|e| *e += 1)
282+
.or_insert(1);
283+
}
284+
285+
// Initialize the binary heap
286+
for (k, v) in map.iter() {
287+
if 2 * *v - 1 > n {
288+
return "".to_string();
289+
} else {
290+
pq.push((*v, *k));
291+
}
292+
}
293+
294+
while !pq.is_empty() {
295+
let (v, k) = pq.pop().unwrap();
296+
ret.push(k);
297+
queue.push_back((v - 1, k));
298+
if queue.len() == 2 {
299+
let (v, k) = queue.pop_front().unwrap();
300+
if v != 0 {
301+
pq.push((v, k));
302+
}
303+
}
304+
}
305+
306+
if ret.len() == n { ret } else { "".to_string() }
307+
}
308+
}
309+
```
310+
263311
### **Go**
264312

265313
```go

solution/0700-0799/0767.Reorganize String/README_EN.md

+48
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,54 @@ public:
220220
};
221221
```
222222

223+
### **Rust**
224+
225+
```rust
226+
use std::collections::{HashMap, BinaryHeap, VecDeque};
227+
228+
impl Solution {
229+
#[allow(dead_code)]
230+
pub fn reorganize_string(s: String) -> String {
231+
let mut map = HashMap::new();
232+
let mut pq = BinaryHeap::new();
233+
let mut ret = String::new();
234+
let mut queue = VecDeque::new();
235+
let n = s.len();
236+
237+
// Initialize the HashMap
238+
for c in s.chars() {
239+
map
240+
.entry(c)
241+
.and_modify(|e| *e += 1)
242+
.or_insert(1);
243+
}
244+
245+
// Initialize the binary heap
246+
for (k, v) in map.iter() {
247+
if 2 * *v - 1 > n {
248+
return "".to_string();
249+
} else {
250+
pq.push((*v, *k));
251+
}
252+
}
253+
254+
while !pq.is_empty() {
255+
let (v, k) = pq.pop().unwrap();
256+
ret.push(k);
257+
queue.push_back((v - 1, k));
258+
if queue.len() == 2 {
259+
let (v, k) = queue.pop_front().unwrap();
260+
if v != 0 {
261+
pq.push((v, k));
262+
}
263+
}
264+
}
265+
266+
if ret.len() == n { ret } else { "".to_string() }
267+
}
268+
}
269+
```
270+
223271
### **Go**
224272

225273
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::collections::{HashMap, BinaryHeap, VecDeque};
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn reorganize_string(s: String) -> String {
6+
let mut map = HashMap::new();
7+
let mut pq = BinaryHeap::new();
8+
let mut ret = String::new();
9+
let mut queue = VecDeque::new();
10+
let n = s.len();
11+
12+
// Initialize the HashMap
13+
for c in s.chars() {
14+
map
15+
.entry(c)
16+
.and_modify(|e| *e += 1)
17+
.or_insert(1);
18+
}
19+
20+
// Initialize the binary heap
21+
for (k, v) in map.iter() {
22+
if 2 * *v - 1 > n {
23+
return "".to_string();
24+
} else {
25+
pq.push((*v, *k));
26+
}
27+
}
28+
29+
while !pq.is_empty() {
30+
let (v, k) = pq.pop().unwrap();
31+
ret.push(k);
32+
queue.push_back((v - 1, k));
33+
if queue.len() == 2 {
34+
let (v, k) = queue.pop_front().unwrap();
35+
if v != 0 {
36+
pq.push((v, k));
37+
}
38+
}
39+
}
40+
41+
if ret.len() == n { ret } else { "".to_string() }
42+
}
43+
}

0 commit comments

Comments
 (0)