FastPrepCan One Dictionary Word Supply the Target Letters?

Can One Dictionary Word Supply the Target Letters?

Compass International Holdings logoCompass International Holdings● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a nonempty string target and an array words, return whether at least one word can supply every letter occurrence of the target.

For this exercise, assume all characters are lowercase English letters and the target must be constructed by rearranging character occurrences from one candidate word. Each occurrence is used at most once. Do not concatenate whole words or combine letters from several candidates.

For this exercise, assume candidates are checked independently. Return false if no candidate supplies the target, including an empty dictionary.

Function

canSupplyTarget(target: String, words: String[]) → boolean

Examples

Example 1

target = "aab"words = ["ab","bbaa"]return = true

This authored example can construct the target from one supplying candidate.

Example 2

target = "aa"words = ["a","ab"]return = false

This authored example cannot reuse the single a in any candidate.

Example 3

target = "ab"words = ["a","b"]return = false

This authored example does not combine letters across different dictionary words.

Constraints

  • For this exercise, assume 1 <= target.length <= 500, 0 <= words.length <= 200 and 1 <= words[i].length <= 2000.
  • All strings contain lowercase English letters.

More Compass International Holdings problems

See Compass International Holdings hiring insights
public boolean canSupplyTarget(String target, String[] words) {
    // Write your code here.
    return false;
}
target"aab"
words["ab","bbaa"]
expectedtrue
Checking account…