This file contains hidden or 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
| removeWord(word) { | |
| if (!word) return false; | |
| let currNode = this.root, | |
| stack = []; | |
| for (const letter of word) { | |
| if (!currNode.children.has(letter)) return false; | |
| currNode = currNode.children.get(letter); | |
| if (word[word.length - 1] !== currNode.value) stack.push(currNode); | |
| } |
This file contains hidden or 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
| getWordsFrom(node = this.root, string = '', array = []) { | |
| if (!node) return; | |
| string += node.value; | |
| if (node.endOfWord) array.push(string); | |
| node.children.forEach((child) => { | |
| this.getWordsFrom(child, string, array); | |
| }); |
This file contains hidden or 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
| findAllWithPrefix(prefix, start = this.root) { | |
| let words = []; | |
| let currNode = this.getLastNode(prefix, start); | |
| if (currNode) { | |
| if (currNode.endOfWord) words.push(prefix); | |
| currNode.children.forEach((child) => | |
| this.getWordsFrom(child, prefix, words) | |
| ); |
This file contains hidden or 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
| getLastNode(letters, start = this.root) { | |
| let currNode = start; | |
| for (const letter of letters) { | |
| if (!currNode.children.has(letter)) return false; | |
| currNode = currNode.children.get(letter); | |
| } | |
| return currNode; | |
| } |
This file contains hidden or 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
| hasWord(word, start = this.root) { | |
| if (!word) return false; | |
| let currNode = start; | |
| for (const letter of word) { | |
| if (!currNode.children.has(letter)) return false; | |
| currNode = currNode.children.get(letter); | |
| } |
This file contains hidden or 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
| insert(word) { | |
| if (!word) return false; | |
| let currNode = this.root; | |
| for (const letter of word) { | |
| if (!currNode.children.has(letter)) { | |
| currNode.children.set(letter,new Node(letter)); | |
| } | |
| currNode = currNode.children.get(letter); |
This file contains hidden or 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
| class Trie { | |
| constructor() { | |
| this.root = new Node(); | |
| } | |
| } |
This file contains hidden or 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
| class Node { | |
| constructor(value = '') { | |
| this.children = new Map(); | |
| this.value = value; | |
| this.endOfWord = false; | |
| } | |
| } |
This file contains hidden or 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
| function rotateNinetyDeg(squareMatrix) { | |
| let left = 0, right = squareMatrix.length - 1; | |
| while (left < right) { | |
| for (let i = 0; i < right - left; i++) { | |
| let top = left, bottom = right; | |
| // save top left | |
| let topLeft = squareMatrix[top][left + i]; | |
| // swap top left and bottom left |
This file contains hidden or 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
| function rotateNinetyDeg(squareMatrix) { | |
| let left = 0, right = squareMatrix.length - 1; | |
| while (left < right) { | |
| for (let i = 0; i < right - left; i++) { | |
| let top = left, bottom = right; | |
| } | |
| left++; | |
| right--; | |
| } |
NewerOlder