Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
CarlaTeo / colorfulNumbers.js
Created May 17, 2021 02:35
[JS] Colorful Numbers
// Source problem: https://algorithms.tutorialhorizon.com/colorful-numbers/
// Objective: Given a number, find out whether its colorful or not.
// Colorful Number: When in a given number, product of every digit of a sub-sequence are different. That number is called Colorful Number.
// Given Number : 3245
// Output : Colorful
// Number 3245 can be broken into parts like 3 2 4 5 32 24 45 324 245.
// this number is a colorful number, since product of every digit of a sub-sequence are different.
// That is, 3 2 4 5 (3*2)=6 (2*4)=8 (4*5)=20, (3*2*4)= 24 (2*4*5)= 40
// Given Number : 326
@CarlaTeo
CarlaTeo / binaryTreeHeight.js
Created May 17, 2021 03:08
[JS] Binary tree height
// Problem: https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem
function getHeight(head) {
if(!head) return -1;
const heights = new Set();
DFS(head, -1, heights);
return Math.max(...heights);
}
@CarlaTeo
CarlaTeo / BSTSmallestGenerator.js
Created May 18, 2021 01:55
[JS ] Binary Search Tree iterator to find the next smallest number
class BSTIterator {
constructor(root) {
this.nextValue = null;
this.sortedValues = getInOrderValues(root);
}
hasNext() {
if(!this.nextValue) {
const next = this.sortedValues.next();
this.nextValue = next.done ? null : next.value;
@CarlaTeo
CarlaTeo / buildBST.js
Created May 24, 2021 02:12
[JS] Build BST from sorted array
function buildBST(sortedArray) {
if(!sortedArray.length) return null;
if(sortedArray.length === 1) return new Node(sortedArray[0]);
const rootIndex = Math.floor(sortedArray.length / 2);
const root = new Node(sortedArray[rootIndex]);
root.left = buildBST(sortedArray.slice(0, rootIndex));
root.right = buildBST(sortedArray.slice(rootIndex + 1));
return root;
@CarlaTeo
CarlaTeo / reverseLinkedList.js
Created May 27, 2021 03:33
[JS] Reverse linked list
function reverseLinkedList(head) {
let prev = null;
let cur = head;
let next = null;
while(cur) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
@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) {
@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 / 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 / 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 / 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;