FastPrepFilter Anagrams of a Target

Filter Anagrams of a Target

Microsoft logoMicrosoft● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given an array of lowercase strings words and a lowercase string target, return every entry of words that is an anagram of target.

Two strings are anagrams when they contain exactly the same letters with the same frequencies. Preserve the original order of matching entries, and preserve duplicate entries.

Function

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

Examples

Example 1

words = ["eat","tea","tan","ate","nat","bat"]target = "aet"return = ["eat","tea","ate"]

eat, tea, and ate have the same letter frequencies as aet. They remain in their input order.

Example 2

words = ["abc","abb","bca","abc"]target = "cab"return = ["abc","bca","abc"]

Both copies of abc and the entry bca match. The nonmatching entry abb is omitted.

Constraints

  • 1 <= words.length <= 10^5
  • 1 <= target.length <= 10^5
  • The total length of all strings in words is at most 2 * 10^5.
  • Every string contains only lowercase English letters.

More Microsoft problems

See Microsoft hiring insights
public String[] filterTargetAnagrams(String[] words, String target) {
  // write your code here
}
words["eat","tea","tan","ate","nat","bat"]
target"aet"
expected["eat", "tea", "ate"]
Checking account…