Skip to content

Commit

Permalink
250th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Nov 8, 2024
1 parent 9f094db commit a1c4f27
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,13 @@ Ace Coding Interview with 75 Qs
[136]: ./src/page-2/136.%20Single%20Number/singleNumber.ts
[1318]: ./src/page-13/1318.%20Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/minFlips.ts

| Trie | | |
| --------------------------------- | --------------- | ------ |
| 208. Implement Trie (Prefix Tree) | [Solution][208] | Medium |
| 1268. Search Suggestions System | Solution | Medium |
| Trie | | |
| --------------------------------- | ---------------- | ------ |
| 208. Implement Trie (Prefix Tree) | [Solution][208] | Medium |
| 1268. Search Suggestions System | [Solution][1268] | Medium |

[208]: ./src/page-2/208.%20Implement%20Trie%20(Prefix%20Tree)/Trie.ts
[1268]: ./src/page-12/1268.%20Search%20Suggestions%20System/suggestedProducts.ts

| Intervals | | |
| ----------------------------------------------- | -------- | ------ |
Expand Down
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 src/page-12/1268. Search Suggestions System/suggestedProducts.ts
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);
};

0 comments on commit a1c4f27

Please sign in to comment.