O(w·n·m + n·m·log n) time | O(n) space, for n products of length up to m and a search word of length w
Intuition
First we need to sort the input to put matching products for any prefix next to each other in lexicographic order.
Then for every prefix we check every product one by one and keep the first three that match.
Algorithm
Sort the products, create result array and prefix string.
Iterate through the outer loop from the first character of searchWord to the last character.
For each outer loop iteration add a new character to prefix, create the matching array and iterate through the inner loop from the first product to the last one.
For each inner loop iteration add the product to matching if it matches prefix and stop early if the length of matching is 3.
After the inner loop stops, add matching to result.
O(n·m·log n) time | O(n) space, for n products of length up to m
Intuition
First we need to sort the input to put matching products for any prefix next to each other in lexicographic order.
Then we create two pointers starting from both ends of the array to form a range of matching products for each prefix.
With every new character added to the prefix, we shrink the range by moving both pointers until the first element matching the prefix is found.
To check if a product matches the prefix we compare only the last added character, because the characters before it already match for every product in the range.
Algorithm
Sort the products, create the result array and put the left and right pointers at the ends of the products.
Iterate through the outer loop from the first character of searchWord to the last character, adding each one to the prefix.
For each outer loop iteration move left right while the character of its product at this position does not match the added one, then move right left the same way.
After both pointers stop, add the array with the first three products between them to result.
O(n·m log n) time | O(n·m) space, for n products of length up to m
Intuition
We can add every product to a trie character by character.
For each new character we create a node in the trie or reuse the existing one, adding the product to that node as a matching prefix.
Then to check what products match the prefix of the search word, we use the saved products at the corresponding node.
To keep all the matches sorted in lexicographic order, we need to sort the products first.
Algorithm
Sort the products, create the result array and the root of the trie.
Iterate through the products and add every one to the trie character by character, appending it to the matching array of each node while the length of that array is less than 3.
Iterate through the outer loop from the first character of searchWord to the last character, taking the corresponding node from the trie.
At each step, add the matching array of the node to result.
The time complexity is the same as for the two pointers approach, because both of them sort the products first. But if we measure only the query time for one prefix, which happens more often than constructing the trie in real life systems, the trie approach is the fastest.