Skip to content

Instantly share code, notes, and snippets.

View ReginaF2012's full-sized avatar

Regina Furness ReginaF2012

View GitHub Profile
@ReginaF2012
ReginaF2012 / js-trie-removeWord
Created March 22, 2021 01:18
removeWord method to be used with trie. Takes in a word. If the trie contains the word it removes the word and returns true. If not, it returns false.
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);
}
@ReginaF2012
ReginaF2012 / js-trie-getWordsFrom
Created March 22, 2021 01:10
getWordsFrom helper method. Recursively gets all of the words in the trie from a starting node.
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);
});
@ReginaF2012
ReginaF2012 / js-trie-findAllWithPrefix
Last active March 22, 2021 01:05
findAllWithPrefix method takes in a prefix and a starting point in the trie, and returns all words from that starting point that contain the prefix
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)
);
@ReginaF2012
ReginaF2012 / js-trie-getLastNode
Created March 22, 2021 00:56
getLastNode helper method for Trie class. Takes in a series of letters and if they are in the trie, returns the node of the last letter.
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;
}
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);
}
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);
class Trie {
constructor() {
this.root = new Node();
}
}
class Node {
constructor(value = '') {
this.children = new Map();
this.value = value;
this.endOfWord = false;
}
}
@ReginaF2012
ReginaF2012 / rotate-square-matrix-js-perform-rotations
Last active March 8, 2021 04:41
Rotating a matrix, in place, 90 degrees clockwise, in JavaScript. Finally performing the rotations
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
@ReginaF2012
ReginaF2012 / rotate-square-matrix-js-top-bottom
Last active March 8, 2021 04:42
Function to rotate a square matrix 90 degrees clockwise in JavaScript, in place. Declaring top and bottom variables.
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--;
}