FastPrepCan a Dictionary Word Be Formed from Note Letters?

Can a Dictionary Word Be Formed from Note Letters?

Atlassian logoAtlassian● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a string note and an array words, return true if at least one dictionary word can be formed by rearranging letters from note. Otherwise return false.

The needed letters may be separated in the note and may appear in any order. Extra note letters are allowed.

For this exercise, assume all characters are lowercase English letters and each note occurrence may be used at most once per candidate. Check words independently: testing one word does not consume letters available to another.

For this exercise, assume every dictionary word is nonempty, the note and dictionary may be empty, and an empty dictionary or an empty note with nonempty words returns false. Do not modify the inputs. The Boolean answer does not depend on dictionary order; do not return a matching word or its index.

Function

canFormAnyWord(note: String, words: String[]) → boolean

Examples

Example 1

note = "tacx"words = ["cat","act"]return = true

This authored example illustrates the reported Boolean check. Both candidates can be formed; the answer is true, regardless of which one occurs first.

Example 2

note = "ab"words = ["aa","ba"]return = true

This authored example illustrates the reported Boolean check. The first candidate needs two a occurrences and fails. The independently checked ba uses one a and one b.

Example 3

note = "xyz"words = ["cat"]return = false

This authored example illustrates the reported Boolean check. No dictionary candidate can be formed from these letters.

Constraints

  • For this exercise, assume 0 <= note.length <= 2000 and 0 <= words.length <= 200.
  • Each word contains from 1 through 500 lowercase English letters.

More Atlassian problems

See Atlassian hiring insights
public boolean canFormAnyWord(String note, String[] words) {
    // write your code here
}
note"tacx"
words["cat","act"]
expectedtrue
Checking account…