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 Node(val, children) { | |
| this.val = val === undefined ? 0 : val; | |
| this.children = children === undefined ? [] : children; | |
| }; | |
| // BFS | |
| function getNOfDescendants(rootNode, targetVal, startNodeIdx, string) { | |
| let nOfDescendants = 0; | |
| const queue = [[rootNode, startNodeIdx === rootNode.val]]; |
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 getPermutations(arr, minOp, original = []) { | |
| const result = []; | |
| for(let size = 2; size <= arr.length; size++) { | |
| for(let i = 0; i + size <= arr.length; i ++) { | |
| const permutation = [ | |
| ...arr.slice(0, i), | |
| ...arr.slice(i, i + size).reverse(), | |
| ...arr.slice(i + size) | |
| ]; | |
| if(!isEqual(original, permutation)) { |
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
| // Solution |s|**2 + |t| | |
| function containsSubstring(str, startIdx, endIdx, substrMap) { | |
| const substrMapCopy = {...substrMap}; | |
| for(let i = startIdx; i <= endIdx; i++) { | |
| const char = str[i]; | |
| if(substrMapCopy[char]) { | |
| substrMapCopy[char] -= 1; | |
| if(substrMapCopy[char] === 0) { | |
| delete substrMapCopy[char]; | |
| if(!Object.keys(substrMapCopy).length) return true; |
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
| const BRACKETS = { | |
| "(": 1, | |
| "[": 2, | |
| "{": 3, | |
| ")": -1, | |
| "]": -2, | |
| "}": -3, | |
| }; | |
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
| // Given a list of N triangles with integer side lengths, determine how many different triangles there are. | |
| // Two triangles are considered to be the same if they can both be placed on the plane such that their vertices | |
| // occupy exactly the same three points. | |
| // https://leetcode.com/discuss/interview-question/922155/facebook-recruiting-portal-counting-triangles | |
| function sortTriangle([a, b, c]) { | |
| if(b > a) { | |
| if(c > b) return [c, b, a]; | |
| else if(c > a) return [b, c, a]; | |
| else return [b, a, c]; |
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 reverseLinkedList(head, lastElement = null) { | |
| let prev = lastElement; | |
| let cur = head; | |
| let next = null; | |
| while(cur != lastElement) { | |
| next = cur.next; | |
| cur.next = prev; | |
| prev = cur; | |
| cur = next; |
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
| // Given a list of integers determine the number of different pairs of elements within it which sum to k. | |
| function numberOfWays(arr, k) { | |
| let counter = 0; | |
| const indexesByModK = {}; | |
| arr.forEach((num, j) => { | |
| const modK = num % k; | |
| const modKComplement = (k - modK) % k; | |
| if(Object.keys(indexesByModK).includes(String(modKComplement))) { | |
| counter += indexesByModK[modKComplement].length; |
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
| // O(n**2) | |
| function countSubarrays(arr) { | |
| const output = arr.map(idx => 1); | |
| for(let i = 0; i < arr.length; i++) { | |
| let stopCount = false; | |
| let max = arr[i]; | |
| for(let j = i + 1; j < arr.length; j++) { | |
| if(!stopCount && arr[i] > arr[j]) { |
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
| LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"; | |
| function isNumber(char) { | |
| return !isNaN(Number(char)); | |
| } | |
| function isLowerCase(char) { | |
| return char === char.toLowerCase(); | |
| } | |
| function rotationalCipher(input, rotationFactor) { |
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 getCharactersDistribution(string) { | |
| const result = {}; | |
| string.split("").forEach((char, idx) => { | |
| if(result[char]) result[char].add(String(idx)); | |
| else result[char] = new Set([String(idx)]); | |
| }); | |
| return result; | |
| } | |
| function matchingPairs(s, t) { |