FastPrepBest Anagram Match

Best Anagram Match

Google logoGoogle● EasyFULLTIMEPHONE SCREEN

Problem statement

Given a lowercase target string and a list of lowercase candidate words, return the lexicographically smallest candidate that is an exact anagram of the target.

An exact anagram has the same length and the same character frequencies. Return the empty string when no candidate qualifies.

Function

findBestAnagram(target: String, candidates: String[]) → String

Examples

Example 1

target = "listen"candidates = ["silent","enlist","google"]return = "enlist"

Both silent and enlist are anagrams; enlist is lexicographically smaller.

Example 2

target = "abc"candidates = ["ab","abd"]return = ""

No candidate has the same multiset.

Constraints

  • 1 <= target.length <= 10^5.
  • 0 <= candidates.length <= 10^5.
  • All strings contain lowercase English letters.

More Google problems

See Google hiring insights
Saved
public String findBestAnagram(String target, String[] candidates) {
  // write your code here
}
Checking account…