diff --git a/solution/0700-0799/0767.Reorganize String/README.md b/solution/0700-0799/0767.Reorganize String/README.md index 2f53700bb84ac..36823852062f2 100644 --- a/solution/0700-0799/0767.Reorganize String/README.md +++ b/solution/0700-0799/0767.Reorganize String/README.md @@ -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 diff --git a/solution/0700-0799/0767.Reorganize String/README_EN.md b/solution/0700-0799/0767.Reorganize String/README_EN.md index 285147a5179d6..c7953363a5596 100644 --- a/solution/0700-0799/0767.Reorganize String/README_EN.md +++ b/solution/0700-0799/0767.Reorganize String/README_EN.md @@ -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 diff --git a/solution/0700-0799/0767.Reorganize String/Solution.rs b/solution/0700-0799/0767.Reorganize String/Solution.rs new file mode 100644 index 0000000000000..3c75da0250df7 --- /dev/null +++ b/solution/0700-0799/0767.Reorganize String/Solution.rs @@ -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() } + } +} \ No newline at end of file