Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rust solution to lc problem: No.0767 #1501

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions solution/0700-0799/0767.Reorganize String/README.md
Original file line number Diff line number Diff line change
@@ -260,6 +260,54 @@ public:
};
```

### **Rust**

```rust
use std::collections::{HashMap, BinaryHeap, VecDeque};

impl Solution {
#[allow(dead_code)]
pub fn reorganize_string(s: String) -> String {
let mut map = HashMap::new();
let mut pq = BinaryHeap::new();
let mut ret = String::new();
let mut queue = VecDeque::new();
let n = s.len();

// Initialize the HashMap
for c in s.chars() {
map
.entry(c)
.and_modify(|e| *e += 1)
.or_insert(1);
}

// Initialize the binary heap
for (k, v) in map.iter() {
if 2 * *v - 1 > n {
return "".to_string();
} else {
pq.push((*v, *k));
}
}

while !pq.is_empty() {
let (v, k) = pq.pop().unwrap();
ret.push(k);
queue.push_back((v - 1, k));
if queue.len() == 2 {
let (v, k) = queue.pop_front().unwrap();
if v != 0 {
pq.push((v, k));
}
}
}

if ret.len() == n { ret } else { "".to_string() }
}
}
```

### **Go**

```go
48 changes: 48 additions & 0 deletions solution/0700-0799/0767.Reorganize String/README_EN.md
Original file line number Diff line number Diff line change
@@ -220,6 +220,54 @@ public:
};
```

### **Rust**

```rust
use std::collections::{HashMap, BinaryHeap, VecDeque};

impl Solution {
#[allow(dead_code)]
pub fn reorganize_string(s: String) -> String {
let mut map = HashMap::new();
let mut pq = BinaryHeap::new();
let mut ret = String::new();
let mut queue = VecDeque::new();
let n = s.len();

// Initialize the HashMap
for c in s.chars() {
map
.entry(c)
.and_modify(|e| *e += 1)
.or_insert(1);
}

// Initialize the binary heap
for (k, v) in map.iter() {
if 2 * *v - 1 > n {
return "".to_string();
} else {
pq.push((*v, *k));
}
}

while !pq.is_empty() {
let (v, k) = pq.pop().unwrap();
ret.push(k);
queue.push_back((v - 1, k));
if queue.len() == 2 {
let (v, k) = queue.pop_front().unwrap();
if v != 0 {
pq.push((v, k));
}
}
}

if ret.len() == n { ret } else { "".to_string() }
}
}
```

### **Go**

```go
43 changes: 43 additions & 0 deletions solution/0700-0799/0767.Reorganize String/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::collections::{HashMap, BinaryHeap, VecDeque};

impl Solution {
#[allow(dead_code)]
pub fn reorganize_string(s: String) -> String {
let mut map = HashMap::new();
let mut pq = BinaryHeap::new();
let mut ret = String::new();
let mut queue = VecDeque::new();
let n = s.len();

// Initialize the HashMap
for c in s.chars() {
map
.entry(c)
.and_modify(|e| *e += 1)
.or_insert(1);
}

// Initialize the binary heap
for (k, v) in map.iter() {
if 2 * *v - 1 > n {
return "".to_string();
} else {
pq.push((*v, *k));
}
}

while !pq.is_empty() {
let (v, k) = pq.pop().unwrap();
ret.push(k);
queue.push_back((v - 1, k));
if queue.len() == 2 {
let (v, k) = queue.pop_front().unwrap();
if v != 0 {
pq.push((v, k));
}
}
}

if ret.len() == n { ret } else { "".to_string() }
}
}