Pin Search Autocomplete
Pinterest users often search for specific pins or boards. To enhance the search experience, an autocomplete feature is crucial. When a user types a few characters, the system should instantly suggest relevant pin titles that match the entered prefix, helping them discover content faster and more accurately. This problem focuses on building the core logic for such a suggestion system.
Implement a function getPinSuggestions that takes a list of existing pin titles and a search prefix as input. Your task is to:
pinTitles.searchPrefix, find all pinTitles from the stored collection that start with this searchPrefix.pinTitles in lexicographical (alphabetical) order.Function Signature:
function getPinSuggestions(pinTitles: string[], searchPrefix: string): string[]
pinTitles: An array of strings, where each string represents a unique title of a pin available on Pinterest.searchPrefix: A string representing the partial input from a user in the search bar.pinTitles that begin with the searchPrefix. The returned titles must be sorted lexicographically.pinTitles: If the pinTitles array is empty, or if no titles match the searchPrefix, return an empty array.searchPrefix: If searchPrefix is an empty string, all pinTitles should be returned, sorted lexicographically.pinTitles in the input array are unique.Examples
Example 1
Input:
pinTitles = ["Fashion Trends", "Food Recipes", "Fitness Motivation", "Furry Friends", "Travel Destinations"], searchPrefix = "F"
Output:
["Fashion Trends","Fitness Motivation","Food Recipes","Furry Friends"]
Note:
Titles starting with 'F' are "Fashion Trends", "Food Recipes", "Fitness Motivation", "Furry Friends". Sorted alphabetically.
Example 2
Input:
pinTitles = ["Cats and Dogs", "Cars and Bikes", "Cooking Ideas", "Creative Crafts"], searchPrefix = "Ca"
Output:
["Cars and Bikes","Cats and Dogs"]
Note:
Titles starting with 'Ca' are "Cats and Dogs", "Cars and Bikes". Sorted alphabetically.
Example 3
Input:
pinTitles = ["Summer Vacation", "Winter Wonderland", "Autumn Leaves"], searchPrefix = "Spr"
Output:
[]
Note:
No pin titles start with the prefix 'Spr'.
Example 4
Input:
pinTitles = ["Art Projects", "Art History", "Artificial Intelligence"], searchPrefix = ""
Output:
["Art History","Art Projects","Artificial Intelligence"]
Note:
An empty prefix should return all titles, sorted lexicographically.
Constraints
1 <= pinTitles.length <= 10^41 <= pinTitles[i].length <= 1000 <= searchPrefix.length <= 100pinTitles and searchPrefix consist of lowercase and uppercase English letters, digits, and spaces.pinTitles will not exceed 10^6.getPinSuggestions with different prefixes (though for this problem, we only call it once).searchPrefix.pinTitles, empty searchPrefix, and no matching results.Loading editor...