Problem · String

Get Search Results

Learn this problem
EasyJPMorgan ChaseOA

Problem statement

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

getSearchResults(words: String[], queries: String[]) → String[][]

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 🌟

Examples

Example 1

words = ["duel", "speed", "dule", "cars"]queries = ["spede", "deul"]return = [["speed"], ["duel", "dule"]]

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

  • 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.

More JPMorgan Chase problems

drafts saved locally
public String[][] getSearchResults(String[] words, String[] queries) {
  // write your code here
}
words["duel", "speed", "dule", "cars"]
queries["spede", "deul"]
expected[["speed", "duel", "dule"]]
checking account