Skip to content

Commit 54e6c13

Browse files
committed
feat: add solutions to lc problem: No.0691
No.0691.Stickers to Spell Word
1 parent 7a82671 commit 54e6c13

File tree

6 files changed

+439
-2
lines changed

6 files changed

+439
-2
lines changed

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

+150-1
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,171 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:BFS + 状态压缩**
54+
5355
<!-- tabs:start -->
5456

5557
### **Python3**
5658

5759
<!-- 这里可写当前语言的特殊实现逻辑 -->
5860

5961
```python
60-
62+
class Solution:
63+
def minStickers(self, stickers: List[str], target: str) -> int:
64+
q = deque([0])
65+
ans = 0
66+
n = len(target)
67+
vis = [False] * (1 << n)
68+
vis[0] = True
69+
while q:
70+
for _ in range(len(q)):
71+
state = q.popleft()
72+
if state == (1 << n) - 1:
73+
return ans
74+
for s in stickers:
75+
nxt = state
76+
cnt = Counter(s)
77+
for i, c in enumerate(target):
78+
if not (nxt & (1 << i)) and cnt[c]:
79+
nxt |= 1 << i
80+
cnt[c] -= 1
81+
if not vis[nxt]:
82+
vis[nxt] = True
83+
q.append(nxt)
84+
ans += 1
85+
return -1
6186
```
6287

6388
### **Java**
6489

6590
<!-- 这里可写当前语言的特殊实现逻辑 -->
6691

6792
```java
93+
class Solution {
94+
public int minStickers(String[] stickers, String target) {
95+
Deque<Integer> q = new ArrayDeque<>();
96+
q.offer(0);
97+
int ans = 0;
98+
int n = target.length();
99+
boolean[] vis = new boolean[1 << n];
100+
vis[0] = true;
101+
while (!q.isEmpty()) {
102+
for (int t = q.size(); t > 0; --t) {
103+
int state = q.poll();
104+
if (state == (1 << n) - 1) {
105+
return ans;
106+
}
107+
for (String s : stickers) {
108+
int nxt = state;
109+
int[] cnt = new int[26];
110+
for (char c : s.toCharArray()) {
111+
++cnt[c - 'a'];
112+
}
113+
for (int i = 0; i < n; ++i) {
114+
int idx = target.charAt(i) - 'a';
115+
if ((nxt & (1 << i)) == 0 && cnt[idx] > 0) {
116+
nxt |= 1 << i;
117+
--cnt[idx];
118+
}
119+
}
120+
if (!vis[nxt]) {
121+
vis[nxt] = true;
122+
q.offer(nxt);
123+
}
124+
}
125+
}
126+
++ans;
127+
}
128+
return -1;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
int minStickers(vector<string>& stickers, string target) {
139+
queue<int> q{{0}};
140+
int ans = 0;
141+
int n = target.size();
142+
vector<bool> vis(1 << n);
143+
vis[0] = true;
144+
while (!q.empty())
145+
{
146+
for (int t = q.size(); t; --t)
147+
{
148+
int state = q.front();
149+
if (state == (1 << n) - 1) return ans;
150+
q.pop();
151+
for (auto& s : stickers)
152+
{
153+
int nxt = state;
154+
vector<int> cnt(26);
155+
for (char& c : s) ++cnt[c - 'a'];
156+
for (int i = 0; i < n; ++i)
157+
{
158+
int idx = target[i] - 'a';
159+
if (!(nxt & (1 << i)) && cnt[idx])
160+
{
161+
nxt |= 1 << i;
162+
--cnt[idx];
163+
}
164+
}
165+
if (!vis[nxt])
166+
{
167+
vis[nxt] = true;
168+
q.push(nxt);
169+
}
170+
}
171+
}
172+
++ans;
173+
}
174+
return -1;
175+
}
176+
};
177+
```
68178
179+
### **Go**
180+
181+
```go
182+
func minStickers(stickers []string, target string) int {
183+
q := []int{0}
184+
n := len(target)
185+
vis := make([]bool, 1<<n)
186+
vis[0] = true
187+
ans := 0
188+
for len(q) > 0 {
189+
for t := len(q); t > 0; t-- {
190+
state := q[0]
191+
if state == (1<<n)-1 {
192+
return ans
193+
}
194+
q = q[1:]
195+
for _, s := range stickers {
196+
nxt := state
197+
cnt := make([]int, 26)
198+
for _, c := range s {
199+
cnt[c-'a']++
200+
}
201+
for i, c := range target {
202+
idx := c - 'a'
203+
if (nxt&(1<<i)) == 0 && cnt[idx] > 0 {
204+
nxt |= 1 << i
205+
cnt[idx]--
206+
}
207+
}
208+
if !vis[nxt] {
209+
vis[nxt] = true
210+
q = append(q, nxt)
211+
}
212+
}
213+
}
214+
ans++
215+
}
216+
return -1
217+
}
69218
```
70219

71220
### **...**

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

+150-1
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,167 @@ We cannot form the target &quot;basicbasic&quot; from cutting letters from the g
4646

4747
## Solutions
4848

49+
BFS.
50+
4951
<!-- tabs:start -->
5052

5153
### **Python3**
5254

5355
```python
54-
56+
class Solution:
57+
def minStickers(self, stickers: List[str], target: str) -> int:
58+
q = deque([0])
59+
ans = 0
60+
n = len(target)
61+
vis = [False] * (1 << n)
62+
vis[0] = True
63+
while q:
64+
for _ in range(len(q)):
65+
state = q.popleft()
66+
if state == (1 << n) - 1:
67+
return ans
68+
for s in stickers:
69+
nxt = state
70+
cnt = Counter(s)
71+
for i, c in enumerate(target):
72+
if not (nxt & (1 << i)) and cnt[c]:
73+
nxt |= 1 << i
74+
cnt[c] -= 1
75+
if not vis[nxt]:
76+
vis[nxt] = True
77+
q.append(nxt)
78+
ans += 1
79+
return -1
5580
```
5681

5782
### **Java**
5883

5984
```java
85+
class Solution {
86+
public int minStickers(String[] stickers, String target) {
87+
Deque<Integer> q = new ArrayDeque<>();
88+
q.offer(0);
89+
int ans = 0;
90+
int n = target.length();
91+
boolean[] vis = new boolean[1 << n];
92+
vis[0] = true;
93+
while (!q.isEmpty()) {
94+
for (int t = q.size(); t > 0; --t) {
95+
int state = q.poll();
96+
if (state == (1 << n) - 1) {
97+
return ans;
98+
}
99+
for (String s : stickers) {
100+
int nxt = state;
101+
int[] cnt = new int[26];
102+
for (char c : s.toCharArray()) {
103+
++cnt[c - 'a'];
104+
}
105+
for (int i = 0; i < n; ++i) {
106+
int idx = target.charAt(i) - 'a';
107+
if ((nxt & (1 << i)) == 0 && cnt[idx] > 0) {
108+
nxt |= 1 << i;
109+
--cnt[idx];
110+
}
111+
}
112+
if (!vis[nxt]) {
113+
vis[nxt] = true;
114+
q.offer(nxt);
115+
}
116+
}
117+
}
118+
++ans;
119+
}
120+
return -1;
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int minStickers(vector<string>& stickers, string target) {
131+
queue<int> q{{0}};
132+
int ans = 0;
133+
int n = target.size();
134+
vector<bool> vis(1 << n);
135+
vis[0] = true;
136+
while (!q.empty())
137+
{
138+
for (int t = q.size(); t; --t)
139+
{
140+
int state = q.front();
141+
if (state == (1 << n) - 1) return ans;
142+
q.pop();
143+
for (auto& s : stickers)
144+
{
145+
int nxt = state;
146+
vector<int> cnt(26);
147+
for (char& c : s) ++cnt[c - 'a'];
148+
for (int i = 0; i < n; ++i)
149+
{
150+
int idx = target[i] - 'a';
151+
if (!(nxt & (1 << i)) && cnt[idx])
152+
{
153+
nxt |= 1 << i;
154+
--cnt[idx];
155+
}
156+
}
157+
if (!vis[nxt])
158+
{
159+
vis[nxt] = true;
160+
q.push(nxt);
161+
}
162+
}
163+
}
164+
++ans;
165+
}
166+
return -1;
167+
}
168+
};
169+
```
60170
171+
### **Go**
172+
173+
```go
174+
func minStickers(stickers []string, target string) int {
175+
q := []int{0}
176+
n := len(target)
177+
vis := make([]bool, 1<<n)
178+
vis[0] = true
179+
ans := 0
180+
for len(q) > 0 {
181+
for t := len(q); t > 0; t-- {
182+
state := q[0]
183+
if state == (1<<n)-1 {
184+
return ans
185+
}
186+
q = q[1:]
187+
for _, s := range stickers {
188+
nxt := state
189+
cnt := make([]int, 26)
190+
for _, c := range s {
191+
cnt[c-'a']++
192+
}
193+
for i, c := range target {
194+
idx := c - 'a'
195+
if (nxt&(1<<i)) == 0 && cnt[idx] > 0 {
196+
nxt |= 1 << i
197+
cnt[idx]--
198+
}
199+
}
200+
if !vis[nxt] {
201+
vis[nxt] = true
202+
q = append(q, nxt)
203+
}
204+
}
205+
}
206+
ans++
207+
}
208+
return -1
209+
}
61210
```
62211

63212
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public:
3+
int minStickers(vector<string>& stickers, string target) {
4+
queue<int> q{{0}};
5+
int ans = 0;
6+
int n = target.size();
7+
vector<bool> vis(1 << n);
8+
vis[0] = true;
9+
while (!q.empty())
10+
{
11+
for (int t = q.size(); t; --t)
12+
{
13+
int state = q.front();
14+
if (state == (1 << n) - 1) return ans;
15+
q.pop();
16+
for (auto& s : stickers)
17+
{
18+
int nxt = state;
19+
vector<int> cnt(26);
20+
for (char& c : s) ++cnt[c - 'a'];
21+
for (int i = 0; i < n; ++i)
22+
{
23+
int idx = target[i] - 'a';
24+
if (!(nxt & (1 << i)) && cnt[idx])
25+
{
26+
nxt |= 1 << i;
27+
--cnt[idx];
28+
}
29+
}
30+
if (!vis[nxt])
31+
{
32+
vis[nxt] = true;
33+
q.push(nxt);
34+
}
35+
}
36+
}
37+
++ans;
38+
}
39+
return -1;
40+
}
41+
};

0 commit comments

Comments
 (0)