Algorithm
Problem Name: 691. Stickers to Spell Word
We are given n
different types of stickers
. Each sticker has a lowercase English word on it.
You would like to spell out the given string target
by cutting individual letters from your collection of stickers and rearranging them. You can use each sticker more than once if you want, and you have infinite quantities of each sticker.
Return the minimum number of stickers that you need to spell out target
. If the task is impossible, return -1
.
Note: In all test cases, all words were chosen randomly from the 1000
most common US English words, and target
was chosen as a concatenation of two random words.
Example 1:
Input: stickers = ["with","example","science"], target = "thehat" Output: 3 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: stickers = ["notice","possible"], target = "basicbasic" Output: -1 Explanation: We cannot form the target "basicbasic" from cutting letters from the given stickers.
Constraints:
n == stickers.length
1 <= n <= 50
1 <= stickers[i].length <= 10
1 <= target.length <= 15
stickers[i]
andtarget
consist of lowercase English letters.
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
const minStickers = function(stickers, target) {
const isEqual = (arr1, arr2) => {
for (let i = 0; i < arr1.length; ++i) if (arr1[i] !== arr2[i]) return false
return true
}
const minus = (arr1, arr2) => {
let res = []
for (let i = 0; i < arr1.length; ++i)
res[i] = arr1[i] <= 0 ? arr1[i] : arr1[i] - arr2[i]
return res
}
const isAllNonpositive = arr => {
return arr.every(item => item <= 0)
}
const getString = arr => {
return arr.reduce((acc, cur, idx) => {
if (cur > 0) return acc + String.fromCharCode(idx + 97).repeat(cur)
else return acc
}, '')
}
let ss = stickers.map(word => {
let tmp = new Array(26).fill(0)
for (let i = 0; i < word.length; ++i) tmp[word.charCodeAt(i) - 97]++
return tmp
})
let root = new Array(26).fill(0)
for (let i = 0; i < target.length; ++i) root[target.charCodeAt(i) - 97]++
let cache = new Set()
let queue = [root]
let size = 0,
level = 0,
front = null
while (queue.length !== 0) {
size = queue.length
while (size--) {
front = queue.shift()
for (let w of ss) {
let t = minus(front, w)
let str = getString(t)
if (isEqual(t, front) || cache.has(str)) continue
if (isAllNonpositive(t)) return level + 1
else {
queue.push(t)
cache.add(str)
}
}
}
level++
}
return -1
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Python Programming
Code -
Python Programming
class Solution:
def minStickers(self, stickers, target):
cnt, res, n = collections.Counter(target), [float("inf")], len(target)
def dfs(dic, used, i):
if i == n:
res[0] = min(res[0], used)
elif dic[target[i]] >= cnt[target[i]]:
dfs(dic, used, i + 1)
elif used < res[0] - 1:
for sticker in stickers:
if target[i] in sticker:
for s in sticker:
dic[s] += 1
dfs(dic, used + 1, i + 1)
for s in sticker:
dic[s] -= 1
dfs(collections.defaultdict(int), 0, 0)
return res[0] < float("inf") and res[0] or -1
Copy The Code &
Try With Live Editor
Input
Output