-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f094db
commit a1c4f27
Showing
3 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/page-12/1268. Search Suggestions System/suggestedProducts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { suggestedProducts } from './suggestedProducts'; | ||
|
||
describe('1268. Search Suggestions System', () => { | ||
test('suggestedProducts', () => { | ||
expect( | ||
suggestedProducts(['mobile', 'mouse', 'moneypot', 'monitor', 'mousepad'], 'mouse'), | ||
).toStrictEqual([ | ||
['mobile', 'moneypot', 'monitor'], | ||
['mobile', 'moneypot', 'monitor'], | ||
['mouse', 'mousepad'], | ||
['mouse', 'mousepad'], | ||
['mouse', 'mousepad'], | ||
]); | ||
|
||
expect(suggestedProducts(['havana'], 'havana')).toStrictEqual([ | ||
['havana'], | ||
['havana'], | ||
['havana'], | ||
['havana'], | ||
['havana'], | ||
['havana'], | ||
]); | ||
}); | ||
}); |
77 changes: 77 additions & 0 deletions
77
src/page-12/1268. Search Suggestions System/suggestedProducts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
class TrieNode { | ||
children: Map<string, TrieNode>; | ||
suggestions: string[]; | ||
|
||
constructor() { | ||
this.children = new Map(); | ||
this.suggestions = []; | ||
} | ||
} | ||
|
||
class Trie { | ||
root: TrieNode; | ||
|
||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
|
||
insert(word: string) { | ||
let node = this.root; | ||
|
||
for (const char of word) { | ||
if (!node.children.has(char)) { | ||
node.children.set(char, new TrieNode()); | ||
} | ||
|
||
node = node.children.get(char) as TrieNode; | ||
|
||
// Keep only top 3 lexicographically minimum products | ||
if (node.suggestions.length < 3) { | ||
node.suggestions.push(word); | ||
} else { | ||
node.suggestions.sort(); | ||
if (word < node.suggestions[2]) { | ||
node.suggestions.pop(); | ||
node.suggestions.push(word); | ||
node.suggestions.sort(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
search(prefix: string): string[][] { | ||
let node = this.root; | ||
|
||
const result: string[][] = []; | ||
|
||
for (const char of prefix) { | ||
if (node) { | ||
node = node.children.get(char) as TrieNode; | ||
} | ||
|
||
result.push(node ? node.suggestions : []); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
type SuggestedProducts = (products: string[], searchWord: string) => string[][]; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const suggestedProducts: SuggestedProducts = (products, searchWord) => { | ||
// Sort products lexicographically | ||
products.sort(); | ||
|
||
const trie = new Trie(); | ||
|
||
// Insert products into Trie | ||
for (const product of products) { | ||
trie.insert(product); | ||
} | ||
|
||
// Get suggestions for each prefix of searchWord | ||
return trie.search(searchWord); | ||
}; |