JavaScript Autocomplete System
Build an autocomplete system that suggests relevant words from a predefined dictionary as a user types into an input field. This is a common feature in many applications, improving user experience by reducing typing effort and helping users find what they're looking for quickly.
Problem Description
You need to implement a JavaScript function that simulates an autocomplete system. This function will take a list of words (the dictionary) and a query string (the user's input) as input. It should return a list of words from the dictionary that start with the given query string.
Key Requirements:
- The system should be case-insensitive. Both the dictionary words and the query string should be treated without regard to case when matching.
- The suggestions should be returned in alphabetical order.
- If no words in the dictionary match the query, an empty array should be returned.
Expected Behavior:
Given a dictionary of words and a user's typed query, the function should identify and return all words from the dictionary that begin with the characters of the query.
Edge Cases to Consider:
- An empty dictionary.
- An empty query string (should return all words in the dictionary, alphabetically sorted).
- A query string that doesn't match any words.
- Words in the dictionary with different casing than the query.
Examples
Example 1:
Input:
dictionary = ["Apple", "Apricot", "Banana", "App", "Avocado"]
query = "app"
Output:
["App", "Apple", "Apricot"]
Explanation:
The words "Apple", "Apricot", and "App" from the dictionary start with "app" (case-insensitive). When sorted alphabetically, they are "App", "Apple", "Apricot". "Banana" and "Avocado" do not start with "app".
Example 2:
Input:
dictionary = ["Cat", "Dog", "Cattle", "Caterpillar"]
query = "Cat"
Output:
["Cat", "Cattle", "Caterpillar"]
Explanation:
All three words start with "Cat" (case-insensitive). Sorted alphabetically, they are "Cat", "Cattle", "Caterpillar".
Example 3:
Input:
dictionary = ["Hello", "World", "JavaScript"]
query = ""
Output:
["Hello", "JavaScript", "World"]
Explanation:
When the query is empty, all words from the dictionary should be returned, sorted alphabetically.
Example 4:
Input:
dictionary = ["Pineapple", "Pear", "Plum"]
query = "z"
Output:
[]
Explanation:
No words in the dictionary start with the letter 'z'.
Constraints
- The dictionary can contain up to 100,000 words.
- Each word in the dictionary and the query string will contain only alphabetic characters (a-z, A-Z).
- The length of each word and the query string will be between 1 and 50 characters, inclusive.
- The implementation should be efficient enough to provide suggestions in near real-time for a large dictionary. Aim for a time complexity that scales well with the number of words and query length.
Notes
- Think about how you can efficiently search through the dictionary.
- Consider data structures that might be beneficial for this type of prefix matching.
- The function signature might look something like
function getAutocompleteSuggestions(dictionary, query).