691. Stickers to Spell Word

Loading Question ...

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.

  • Seen this question in a real interview before?  
    Yes
    No
    When did you encounter this question?  
    last week
    last month
    last 3 month
    last 6 month
    more than 6 months
    other
    Which stage?  
    Online Assessment
    Phone Interview
    Onsite Interview
    Don't know
    Which company?  
    Adobe
    Aetion
    Airbnb
    Alibaba
    Amazon
    Apple
    Baidu
    Bloomberg
    Cloudera
    Coursera
    Cryptic Studios
    Dropbox
    eBay
    Epic Systems
    Expedia
    Facebook
    Fallible
    Games
    Gilt Groupe
    GoDaddy
    Goo
    Google
    Hedvig
    Helix
    Houzz
    Hulu
    Indeed
    Intuit
    IXL
    LinkedIn
    LiveRamp
    Lyft
    MAQ Software
    Mathworks
    Microsoft
    NetEase
    Netsuite
    Palantir
    Palantir Technologies
    Pinterest
    Pocket Gems
    Poynt
    Pure
    Pure Storage
    Rackspace
    Riot
    Riot Games
    Samsung
    Snapchat
    Square
    Storage
    Tencent
    TinyCo
    Twitter
    Two Sigma
    Uber
    Valve
    WAP
    Works Applications
    Yahoo
    Yahoo!
    Yelp
    Zappos
    Zenefits