FastPrepFirst Dictionary Word Containing Target Letters

First Dictionary Word Containing Target Letters

Atlassian logoAtlassian● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a nonempty string target and an array words, return the first dictionary word whose characters can form the target. Extra characters in the dictionary word are allowed.

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. First means the smallest dictionary index. Return the empty string if no candidate supplies the target, including an empty dictionary.

The source sample and prose establish this containment direction; its displayed unfinished attempt checks the opposite direction. This exercise follows the prose and sample.

Function

firstContainingWord(target: String, words: String[]) → String

Examples

Example 1

target = "sky"words = ["cry","edt","srk","srrfkrey"]return = "srrfkrey"

The original Atlassian sample selects srrfkrey, which supplies s, k and y even with extra letters.

Example 2

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

This authored example requires two a occurrences and one b; the first supplying word is bbaa.

Example 3

target = "abc"words = ["ab","bc"]return = ""

This authored example has no supplying candidate.

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 Atlassian problems

See Atlassian hiring insights
public String firstContainingWord(String target, String[] words) {
    // Write your code here.
    return "";
}
target"sky"
words["cry","edt","srk","srrfkrey"]
expected"srrfkrey"
Checking account…