Beruflich Dokumente
Kultur Dokumente
com/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/
We are given N different types of stickers. Each sticker has a lowercase English word on it.
User Accepted: 81
You would like to spell out the given target string by cutting individual letters from your collection of stickers and rearranging
them. User Tried: 374
You can use each sticker more than once if you want, and you have infinite quantities of each sticker.
Total Accepted: 86
What is the minimum number of stickers that you need to spell out the target ? If the task is impossible, return -1.
Output:
Explanation:
Example 2:
Input:
Output:
-1
Explanation:
We can't form the target "basicbasic" from cutting letters from the given stickers.
Note:
Discuss (https://discuss.leetcode.com/category/1546)
Python textmate
1 class Solution(object):
2 def minStickers(self, stickers, target):
3 """
4 :type stickers: List[str]
5 :type target: str
6 :rtype: int
7 """
8 if not stickers:
9 return 0
10
11 if not target:
12 return -1
13
14
15
16 e1 = 0
17 dicta = {}
18 Iteration = True
19 while Iteration == True:
20 Found = True
21 f1 = 0
22 while Found == True:
https://leetcode.com/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/ 1/2
10/8/2017 https://leetcode.com/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/
23 if target[e1] in stickers[f1]:
24 if stickers[f1] not in dicta:
25 dicta[stickers[f1]] = 1
26 Found = False
27 else:
28 dicta[stickers[f1]] += 1
29 Found = False
30 if f1 == len(stickers) -1 and Found == True:
31 return -1
32 f1 += 1
33 e1 += 1
34 if e1 == len(target):
35 Iteration = False
36
37 print(dicta)
38 result = 0
39 for value in dicta:
40 result += dicta[value]
41 return result
42
43
Custom Testcase
Privacy
https://leetcode.com/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/ 2/2