Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
CarlaTeo / nodesInSubTree.js
Last active June 28, 2021 02:23
[JS] Nodes in a Subtree
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]];
@CarlaTeo
CarlaTeo / minPerms.js
Created June 9, 2021 03:16
[JS] Minimize permutations
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)) {
@CarlaTeo
CarlaTeo / minLenSubString.js
Last active June 27, 2021 23:06
[JS] Minimum Length Substrings
// 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;
@CarlaTeo
CarlaTeo / brackets.js
Created June 4, 2021 02:36
[JS] Balanced brackets
const BRACKETS = {
"(": 1,
"[": 2,
"{": 3,
")": -1,
"]": -2,
"}": -3,
};
@CarlaTeo
CarlaTeo / distinctTriangles.js
Last active June 28, 2021 03:05
[JS] Count distinct triagles
// 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];
@CarlaTeo
CarlaTeo / reverseLinkedListSubparts.js
Created May 30, 2021 04:11
[JS] Reverse linked list even subparts
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;
@CarlaTeo
CarlaTeo / pairSums.js
Created May 30, 2021 04:07
[JS] Pair sums
// 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;
@CarlaTeo
CarlaTeo / countSubarrays.js
Last active June 27, 2021 05:05
[JS] Count contiguous subarrays
// 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]) {
@CarlaTeo
CarlaTeo / rotationalCipher.js
Created May 30, 2021 04:04
[JS] Rotational cipher
LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
function isNumber(char) {
return !isNaN(Number(char));
}
function isLowerCase(char) {
return char === char.toLowerCase();
}
function rotationalCipher(input, rotationFactor) {
@CarlaTeo
CarlaTeo / matchingPairs.js
Created May 30, 2021 04:00
[JS] Matching pairs
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) {