Skip to content

Commit 93ff844

Browse files
committed
feat: add rust solution to lc problem: No.0691
No.0691.Stickers to Spell Word
1 parent 65d26a5 commit 93ff844

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

solution/0600-0699/0691.Stickers to Spell Word/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,51 @@ func minStickers(stickers []string, target string) int {
217217
}
218218
```
219219

220+
### **Rust**
221+
222+
```rust
223+
use std::collections::{HashSet, VecDeque};
224+
225+
impl Solution {
226+
pub fn min_stickers(stickers: Vec<String>, target: String) -> i32 {
227+
let mut q = VecDeque::new();
228+
q.push_back(0);
229+
let mut ans = 0;
230+
let n = target.len();
231+
let mut vis = HashSet::new();
232+
vis.insert(0);
233+
while !q.is_empty() {
234+
for _ in 0..q.len() {
235+
let state = q.pop_front().unwrap();
236+
if state == (1 << n) - 1 {
237+
return ans;
238+
}
239+
for s in &stickers {
240+
let mut nxt = state;
241+
let mut cnt = [0; 26];
242+
for &c in s.as_bytes() {
243+
cnt[(c - b'a') as usize] += 1;
244+
}
245+
for (i, &c) in target.as_bytes().iter().enumerate() {
246+
let idx = (c - b'a') as usize;
247+
if (nxt & (1 << i)) == 0 && cnt[idx] > 0 {
248+
nxt |= 1 << i;
249+
cnt[idx] -= 1;
250+
}
251+
}
252+
if !vis.contains(&nxt) {
253+
q.push_back(nxt);
254+
vis.insert(nxt);
255+
}
256+
}
257+
}
258+
ans += 1;
259+
}
260+
-1
261+
}
262+
}
263+
```
264+
220265
### **...**
221266

222267
```

solution/0600-0699/0691.Stickers to Spell Word/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,51 @@ func minStickers(stickers []string, target string) int {
209209
}
210210
```
211211

212+
### **Rust**
213+
214+
```rust
215+
use std::collections::{HashSet, VecDeque};
216+
217+
impl Solution {
218+
pub fn min_stickers(stickers: Vec<String>, target: String) -> i32 {
219+
let mut q = VecDeque::new();
220+
q.push_back(0);
221+
let mut ans = 0;
222+
let n = target.len();
223+
let mut vis = HashSet::new();
224+
vis.insert(0);
225+
while !q.is_empty() {
226+
for _ in 0..q.len() {
227+
let state = q.pop_front().unwrap();
228+
if state == (1 << n) - 1 {
229+
return ans;
230+
}
231+
for s in &stickers {
232+
let mut nxt = state;
233+
let mut cnt = [0; 26];
234+
for &c in s.as_bytes() {
235+
cnt[(c - b'a') as usize] += 1;
236+
}
237+
for (i, &c) in target.as_bytes().iter().enumerate() {
238+
let idx = (c - b'a') as usize;
239+
if (nxt & (1 << i)) == 0 && cnt[idx] > 0 {
240+
nxt |= 1 << i;
241+
cnt[idx] -= 1;
242+
}
243+
}
244+
if !vis.contains(&nxt) {
245+
q.push_back(nxt);
246+
vis.insert(nxt);
247+
}
248+
}
249+
}
250+
ans += 1;
251+
}
252+
-1
253+
}
254+
}
255+
```
256+
212257
### **...**
213258

214259
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::collections::{HashSet, VecDeque};
2+
3+
impl Solution {
4+
pub fn min_stickers(stickers: Vec<String>, target: String) -> i32 {
5+
let mut q = VecDeque::new();
6+
q.push_back(0);
7+
let mut ans = 0;
8+
let n = target.len();
9+
let mut vis = HashSet::new();
10+
vis.insert(0);
11+
while !q.is_empty() {
12+
for _ in 0..q.len() {
13+
let state = q.pop_front().unwrap();
14+
if state == (1 << n) - 1 {
15+
return ans;
16+
}
17+
for s in &stickers {
18+
let mut nxt = state;
19+
let mut cnt = [0; 26];
20+
for &c in s.as_bytes() {
21+
cnt[(c - b'a') as usize] += 1;
22+
}
23+
for (i, &c) in target.as_bytes().iter().enumerate() {
24+
let idx = (c - b'a') as usize;
25+
if (nxt & (1 << i)) == 0 && cnt[idx] > 0 {
26+
nxt |= 1 << i;
27+
cnt[idx] -= 1;
28+
}
29+
}
30+
if !vis.contains(&nxt) {
31+
q.push_back(nxt);
32+
vis.insert(nxt);
33+
}
34+
}
35+
}
36+
ans += 1;
37+
}
38+
-1
39+
}
40+
}

0 commit comments

Comments
 (0)