FastPrepFastPrep
Problem Brief

Get Search Results

OA

Implement an autocorrect function that returns all words which are anagrams of a search query. An anagram is any string that can be formed by rearranging the letters of another string.

Given two arrays, words and queries of length n and q, respectively, for each query, return an array of strings that are anagrams of the query, sorted in alphabetical order.

Function Description

Complete the function getSearchResults in the editor with the following arguments:

  • string words[n]: the list of words to search
  • string queries[q]: the words to search for

Returns

string[q][]: the results for each search query

Grateful beyond words to a wonderful old friend for your kind and generous help 🌟

1Example 1

Input
words = ["duel", "speed", "dule", "cars"], queries = ["spede", "deul"]
Output
[["speed"], ["duel", "dule"]]
Explanation

With words = ["duel", "speed", "dule", "cars"] and queries = ["spede", "deul"]:

  • For "spede", the only anagram is "speed"
  • For "deul", the anagrams are "duel" and "dule"

Return [["speed"], ["duel", "dule"]].

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n, q ≤ 5000
  • 1 ≤ length of words[i], length of queries[i] ≤ 100
  • It is guaranteed that each query word has at least one anagram in words.
public String[][] getSearchResults(String[] words, String[] queries) {
  // write your code here
}
Input

words

["duel", "speed", "dule", "cars"]

queries

["spede", "deul"]

Output

[["speed", "duel", "dule"]]

Sign in to submit your solution.