码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Stickers to Spell Word 拼单词

时间:2018-02-25 01:11:30      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:rand   type   number   range   com   xpl   note   form   turn   

 

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 target string 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.

What is the minimum number of stickers that you need to spell out the target? If the task is impossible, return -1.

Example 1:

Input:

["with", "example", "science"], "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:

["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.

 

s

 

[LeetCode] Stickers to Spell Word 拼单词

标签:rand   type   number   range   com   xpl   note   form   turn   

原文地址:https://www.cnblogs.com/grandyang/p/8468045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!