★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-cbnnason-me.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We are given N different types of stickers. Each sticker has a lowercase English word on it.git
You would like to spell out the given target
string by cutting individual letters from your collection of stickers and rearranging them.github
You can use each sticker more than once if you want, and you have infinite quantities of each sticker.数组
What is the minimum number of stickers that you need to spell out the target
? If the task is impossible, return -1.微信
Example 1:app
Input:dom
["with", "example", "science"], "thehat"
Output:函数
3
Explanation:post
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.咱们给出了 N 种不一样类型的贴纸。每一个贴纸上都有一个小写的英文单词。
你但愿从本身的贴纸集合中裁剪单个字母并从新排列它们,从而拼写出给定的目标字符串 target
。
若是你愿意的话,你能够不止一次地使用每一张贴纸,并且每一张贴纸的数量都是无限的。
拼出目标 target
所需的最小贴纸数量是多少?若是任务不可能,则返回 -1。
示例 1:
输入:
["with", "example", "science"], "thehat"
输出:
3
解释:
咱们可使用 2 个 "with" 贴纸,和 1 个 "example" 贴纸。 把贴纸上的字母剪下来并从新排列后,就能够造成目标 “thehat“ 了。 此外,这是造成目标字符串所需的最小贴纸数量。
示例 2:
输入:
["notice", "possible"], "basicbasic"
输出:
-1
解释:
咱们不能经过剪切给定贴纸的字母来造成目标“basicbasic”。
提示:
stickers
长度范围是 [1, 50]
。stickers
由小写英文单词组成(不带撇号)。target
的长度在 [1, 15]
范围内,由小写字母组成。1 class Solution { 2 func minStickers(_ stickers: [String], _ target: String) -> Int { 3 var N:Int = stickers.count 4 var freq:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:26),count:N) 5 var memo:[String:Int] = [String:Int]() 6 memo[""] = 0 7 for i in 0..<N 8 { 9 for char in stickers[i].characters 10 { 11 freq[i][char.ascii - 97] += 1 12 } 13 } 14 return helper(&freq, target, &memo) 15 } 16 17 func helper(_ freq:inout [[Int]], _ target: String,_ memo:inout [String:Int]) -> Int 18 { 19 if let num:Int = memo[target] 20 { 21 return num 22 } 23 var res:Int = Int.max 24 var N:Int = freq.count 25 var cnt:[Int] = [Int](repeating:0,count:26) 26 for char in target.characters 27 { 28 cnt[char.ascii - 97] += 1 29 } 30 for i in 0..<N 31 { 32 if freq[i][target[0].ascii - 97] == 0 33 { 34 continue 35 } 36 var t:String = String() 37 for j in 0..<26 38 { 39 if cnt[j] - freq[i][j] > 0 40 { 41 for i in 0..<(cnt[j] - freq[i][j]) 42 { 43 t.append((97 + j).ASCII) 44 } 45 46 } 47 } 48 var ans:Int = helper(&freq, t, &memo) 49 if ans != -1 {res = min(res, ans + 1)} 50 } 51 memo[target,default:0] = (res == Int.max) ? -1 : res 52 return memo[target,default:0] 53 } 54 } 55 56 //String扩展 57 extension String { 58 //subscript函数能够检索数组中的值 59 //直接按照索引方式截取指定索引的字符 60 subscript (_ i: Int) -> Character { 61 //读取字符 62 get {return self[index(startIndex, offsetBy: i)]} 63 } 64 } 65 66 //Character扩展 67 extension Character 68 { 69 //Character转ASCII整数值(定义小写为整数值) 70 var ascii: Int { 71 get { 72 return Int(self.unicodeScalars.first?.value ?? 0) 73 } 74 } 75 } 76 77 //Int扩展 78 extension Int 79 { 80 //Int转Character,ASCII值(定义大写为字符值) 81 var ASCII:Character 82 { 83 get {return Character(UnicodeScalar(self)!)} 84 } 85 }