Skip to content

Instantly share code, notes, and snippets.

View tatsuyax25's full-sized avatar
πŸ‘¨β€πŸ’»
Focusing

Miguel Urena tatsuyax25

πŸ‘¨β€πŸ’»
Focusing
View GitHub Profile
@tatsuyax25
tatsuyax25 / maxDifference.js
Created June 11, 2025 17:12
You are given a string s and an integer k. Your task is to find the maximum difference between the frequency of two characters, freq[a] - freq[b], in a substring subs of s, such that: subs has a size of at least k. Character a has an odd frequency i
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var maxDifference = function(s, k) {
const n = s.length;
// Convert each character to a digit (0-4 for faster lookup)
const digits = new Uint8Array(n);
@tatsuyax25
tatsuyax25 / maxDifference.js
Created June 10, 2025 20:15
You are given a string s consisting of lowercase English letters. Your task is to find the maximum difference diff = freq(a1) - freq(a2) between the frequency of characters a1 and a2 in the string such that: a1 has an odd frequency in the string. a
/**
* @param {string} s
* @return {number}
*/
var maxDifference = function(s) {
// Step 1: Count frequencies of each character
const freqMap = {}; // Object to store frequency of each character
for (const char of s) {
freqMap[char] = (freqMap[char] || 0) + 1; // Increment frequency count
@tatsuyax25
tatsuyax25 / clearStars.js
Created June 7, 2025 15:37
You are given a string s. It may contain any number of '*' characters. Your task is to remove all '*' characters. While there is a '*', do the following operation: Delete the leftmost '*' and the smallest non-'*' character to its left. If there are
/**
* @param {string} s
* @return {string}
*/
var clearStars = function(s) {
// Array to store indices of characters ('a' to 'z')
const indices = Array.from({ length: 26 }, () => []);
// Convert the string into an array for easier modifications
const res = s.split('');
@tatsuyax25
tatsuyax25 / robotWithString.
Created June 6, 2025 15:45
You are given a string s and a robot that currently holds an empty string t. Apply one of the following operations until s and t are both empty: Remove the first character of a string s and give it to the robot. The robot will append this character
/**
* @param {string} s
* @return {string}
*/
var robotWithString = function(s) {
let ans = ""; // Stores the final result string
let stack = []; // Acts as a temporary storage for characters
let freq = new Array(26).fill(0); // Keeps track of character frequencies
// Populate frequency array for all characters in the string
@tatsuyax25
tatsuyax25 / smallestEquivalentString .js
Created June 5, 2025 17:20
You are given two strings of the same length s1 and s2 and a string baseStr. We say s1[i] and s2[i] are equivalent characters. For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'. Equivalent characters fol
/**
* @param {string} s1
* @param {string} s2
* @param {string} baseStr
* @return {string}
*/
class UnionFind {
constructor() {
this.parent = Array.from({ length: 26 }, (_, i) => i); // 'a' to 'z' mapped to 0-25
}
@tatsuyax25
tatsuyax25 / answerString.js
Created June 4, 2025 17:59
You are given a string word, and an integer numFriends. Alice is organizing a game for her numFriends friends. There are multiple rounds in the game, where in each round: word is split into numFriends non-empty strings, such that no previous round
/**
* Finds the lexicographically largest substring based on the number of friends.
* If `numFriends` is 1, the whole word is returned.
* Otherwise, the function finds the best substring by iterating through possible slices.
*
* @param {string} word - The input word.
* @param {number} numFriends - The number of friends involved in the selection process.
* @return {string} - The lexicographically largest substring.
*/
var answerString = function (word, numFriends) {
@tatsuyax25
tatsuyax25 / maxCandies.js
Created June 3, 2025 19:56
You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where: status[i] is 1 if the ith box is open and 0 if the ith box is closed, candies[i] is the number of candies in the ith box, keys[i] i
/**
* @param {number[]} status - Array indicating whether each box is open (1) or closed (0).
* @param {number[]} candies - Number of candies in each box.
* @param {number[][]} keys - List of keys contained in each box.
* @param {number[][]} containedBoxes - List of boxes contained in each box.
* @param {number[]} initialBoxes - Labels of the boxes you start with.
* @return {number} - Maximum number of candies you can collect.
*/
var maxCandies = function(status, candies, keys, containedBoxes, initialBoxes) {
const queue = [...initialBoxes]; // Queue to process available boxes
@tatsuyax25
tatsuyax25 / candy.js
Created June 2, 2025 16:18
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children w
/**
* @param {number[]} ratings
* @return {number}
*/
var candy = function(ratings) {
const length = ratings.length;
// Initialize candies array, giving each child at least one candy.
const candies = new Array(length).fill(1);
@tatsuyax25
tatsuyax25 / distributeCandies.js
Created June 1, 2025 16:07
You are given two positive integers n and limit. Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
/**
* Calculates the number of ways to distribute `n` candies among 3 children
* ensuring no child gets more than `limit` candies.
*
* @param {number} n - Total number of candies.
* @param {number} limit - Maximum candies each child can receive.
* @return {number} - The number of valid distributions.
*/
var distributeCandies = function(n, limit) {
let count = 0; // Stores the total number of valid distributions.
@tatsuyax25
tatsuyax25 / closestMeetingNode.js
Created May 30, 2025 16:02
You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node ed
/**
* Finds the closest meeting node between two starting nodes in a directed graph.
*
* @param {number[]} edges - Array representing directed edges of the graph.
* @param {number} node1 - First starting node.
* @param {number} node2 - Second starting node.
* @return {number} - The closest meeting node or -1 if no valid meeting node exists.
*/
// Function to initialize an adjacency list representation of the graph