Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
:octocat:
Focusing

Miguel Urena tatsuyax25

:octocat:
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / spellchecker.js
Created September 14, 2025 16:41
Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. For a given query word, the spell checker handles two categories of spelling mistakes: Capitalization: If the query matches a word in the wordlist
/**
* @param {string[]} wordlist
* @param {string[]} queries
* @return {string[]}
*/
var spellchecker = function(wordlist, queries) {
// Helper function to normalize vowels in a word
const normalizeVowels = (word) => {
return word.toLowerCase().replace(/[aeiou]/g, '*');
};
@tatsuyax25
tatsuyax25 / maxFreqSum.js
Created September 13, 2025 21:14
You are given a string s consisting of lowercase English letters ('a' to 'z'). Your task is to: Find the vowel (one of 'a', 'e', 'i', 'o', or 'u') with the maximum frequency. Find the consonant (all other letters excluding vowels) with the maximum
/**
* @param {string} s
* @return {number}
*/
var maxFreqSum = function(s) {
// Step 1: Define vowels for easy lookup
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
// Step 2: Create frequency maps for vowels and consonants
const vowelFreq = {};
@tatsuyax25
tatsuyax25 / doesAliceWin.js
Last active September 12, 2025 19:41
Alice and Bob are playing a game on a string. You are given a string s, Alice and Bob will take turns playing the following game where Alice starts first: On Alice's turn, she has to remove any non-empty substring from s that contains an odd number
/**
* @param {string} s
* @return {boolean}
*/
var doesAliceWin = function(s) {
// Define a set containing all lowercase English vowels
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
let prefixParity = new Set();
// Initialize a counter to track the number of vowels in the string
let count = 0;
@tatsuyax25
tatsuyax25 / sortVowels.js
Last active September 11, 2025 15:39
Given a 0-indexed string s, permute s to get a new string t such that: All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i]. The vowels must be s
/**
* Sorts all vowels in the input string in non-decreasing order,
* while keeping consonants and other characters in their original positions.
*
* @param {string} s - The input string to process.
* @return {string} - The resulting string with sorted vowels.
*/
const sortVowels = function(s) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
@tatsuyax25
tatsuyax25 / minimumTeachings.js
Created September 10, 2025 20:16
On a social network consisting of m users and some friendships between users, two users can communicate with each other if they know a common language. You are given an integer n, an array languages, and an array friendships where: There are n lang
/**
* @param {number} n
* @param {number[][]} languages
* @param {number[][]} friendships
* @return {number}
*/
var minimumTeachings = function(n, languages, friendships) {
// 1) Convert each user's language list to a Set for 0(1) checks.
// We'll store with 1-based alignment: user i => index i (ignore index 0).
const userLangs = Array(languages.length + 1);
@tatsuyax25
tatsuyax25 / peopleAwareOfSecret.js
Created September 9, 2025 19:13
On day 1, one person discovers a secret. You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the secret. You are also given an integer forget, whi
/**
* @param {number} n
* @param {number} delay
* @param {number} forget
* @return {number}
*/
var peopleAwareOfSecret = function(n, delay, forget) {
const MOD = 1e9 + 7;
// dp[i] = number of people who learn the secret on day i
@tatsuyax25
tatsuyax25 / getNoZeroIntegers.js
Created September 8, 2025 19:53
No-Zero integer is a positive integer that does not contain any 0 in its decimal representation. Given an integer n, return a list of two integers [a, b] where: a and b are No-Zero integers. a + b = n The test cases are generated so that there is a
/**
* Helper function to check if a number is a No-Zero integer
* A No-Zero integer has no '0' digit in its decimal representation
*/
function isNoZero(num) {
return !num.toString().includes('0');
};
/**
* Main function to find two No-Zero integers that sum to n
@tatsuyax25
tatsuyax25 / sumZero.js
Created September 7, 2025 16:58
Given an integer n, return any array containing n unique integers such that they add up to 0.
/**
* @param {number} n
* @return {number[]}
*/
var sumZero = function(n) {
const result = [];
// Add symmetric pairs: [-1, 1], [-2, 2], ...
for (let i = 1; i <= Math.floor(n / 2); i++) {
result.push(-i, i)
@tatsuyax25
tatsuyax25 / minOperations.js
Created September 6, 2025 14:51
You are given a 2D array queries, where queries[i] is of the form [l, r]. Each queries[i] defines an array of integers nums consisting of elements ranging from l to r, both inclusive. In one operation, you can: Select two integers a and b from the
/**
* Calculates the minimum number of operations required to reduce
* all elements in each query range [l, r] to zero.
*
* In one operation, select two numbers and replace them with floor(a / 4) and floor(b / 4).
*
* This solution groups numbers by their "division depth" — how many times
* they must be divided by 4 before reaching 0 — and counts how many numbers
* fall into each depth level.
*
@tatsuyax25
tatsuyax25 / makeTheIntegerZero.js
Created September 5, 2025 18:49
You are given two integers num1 and num2. In one operation, you can choose integer i in the range [0, 60] and subtract 2i + num2 from num1. Return the integer denoting the minimum number of operations needed to make num1 equal to 0. If it is impos
/**
* Determines the minimum number of operations needed to reduce num1 to 0.
* Each operation subtracts (2^i + num2), where i ∈ [0, 60].
* Returns -1 if it's impossible.
*
* @param {number} num1 - Starting value
* @param {number} num2 - Fixed offset added to each power of 2
* @return {number} - Minimum number of operations or -1
*/
var makeTheIntegerZero = function(num1, num2) {