First Dictionary Word from Note Letters
Problem statement
Given a string note and an ordered array words, return the first word that can be formed by rearranging letters from note. The first word means the one with the smallest index in the input array.
For this exercise, assume all characters are lowercase English letters, every word is nonempty, and each character occurrence in note may be used at most once to form a candidate word. Candidates are checked independently, so testing one word does not consume letters for later words.
For this exercise, assume an empty string is returned if no word can be formed. The note and the dictionary may be empty.
Function
firstWord(note: String, words: String[]) → StringExamples
Example 1
note = "balloons"words = ["son","ball","friends"]return = "son"The source example returns son because it is the first constructible dictionary word; later ball is also constructible.
Example 2
note = "aab"words = ["baa","aaa","ab"]return = "baa"Reordering is allowed and each character occurrence is used at most once; baa is first.
Constraints
- For this exercise, assume
0 <= note.length <= 10^4. 0 <= words.length <= 1000.- Each word length is from
1through1000. - All characters are from
athroughz.