Sei sulla pagina 1di 2

10/8/2017 https://leetcode.

com/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/

691. Stickers to Spell Word

My Submissions (/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/submissions/) Back to Contest (/contest/leetcode-weekly-contest-53/)

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.

Example 1: Total Submissions: 948

Input: Difficulty: Hard

["with", "example", "science"], "thehat"

Output:

Explanation:

We can use 2 "with" stickers, and 1 "example" sticker.


After cutting and rearrange the letters of those stickers, we can form the target "thehat".
Also, this is the minimum number of stickers necessary to form the target string.

Example 2:

Input:

["notice", "possible"], "basicbasic"

Output:

-1

Explanation:

We can't form the target "basicbasic" from cutting letters from the given stickers.

Note:

stickers has length in the range [1, 50] .


stickers consists of lowercase English words (without apostrophes).
target has length in the range [1, 15] , and consists of lowercase English letters.
In all test cases, all words were chosen randomly from the 1000 most common US English words, and the target was chosen as a concatenation of two random words.
The time limit may be more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms on average.

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

Run Code Submit Solution

Copyright 2017 LeetCode


Contact Us | Students (/students) | Frequently Asked Questions (/faq/) | Terms of Service (/tos/)

Privacy

https://leetcode.com/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/ 2/2

Potrebbero piacerti anche