From ec7752f9f0d6fc25a4637a683cc960e02e73454f Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Tue, 18 Aug 2026 11:18:20 -0400 Subject: [PATCH] feat(work): add solutions for LeetCode problems 217, 242, 189, and 49 --- work/Easy/Array/217.contains-duplicate.js | 62 ++++++++++++++++++++ work/Easy/Hash Table/242.valid-anagram.js | 55 ++++++++++++++++++ work/Medium/Array/189.rotate-array.js | 57 +++++++++++++++++++ work/Medium/Array/49.group-anagrams.js | 69 +++++++++++++++++++++++ 4 files changed, 243 insertions(+) create mode 100644 work/Easy/Array/217.contains-duplicate.js create mode 100644 work/Easy/Hash Table/242.valid-anagram.js create mode 100644 work/Medium/Array/189.rotate-array.js create mode 100644 work/Medium/Array/49.group-anagrams.js diff --git a/work/Easy/Array/217.contains-duplicate.js b/work/Easy/Array/217.contains-duplicate.js new file mode 100644 index 0000000..a9c6c9b --- /dev/null +++ b/work/Easy/Array/217.contains-duplicate.js @@ -0,0 +1,62 @@ +/* + * 217. Contains Duplicate + * Difficulty: Easy + * https://leetcode.com/problems/contains-duplicate/ + * + * ────────────────────────────────────────────────── + * + * Given an integer array nums, return true if any value appears at + * least twice in the array, and return false if every element is + * distinct. + * + * + * + * Example 1: + * + * Input: nums = [1,2,3,1] + * + * Output: true + * + * Explanation: + * + * The element 1 occurs at the indices 0 and 3. + * + * Example 2: + * + * Input: nums = [1,2,3,4] + * + * Output: false + * + * Explanation: + * + * All elements are distinct. + * + * Example 3: + * + * Input: nums = [1,1,1,3,3,4,3,2,4,2] + * + * Output: true + * + * + * + * Constraints: + * + * • 1 <= nums.length <= 10^5 + * + * • -10^9 <= nums[i] <= 10^9 +*/ + +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + const freq = {}; + for (let n of nums) { + freq[n] = (freq[n] || 0) + 1; + if (freq[n] >= 2) { + return true; + } + } + return false; +}; diff --git a/work/Easy/Hash Table/242.valid-anagram.js b/work/Easy/Hash Table/242.valid-anagram.js new file mode 100644 index 0000000..aace0ca --- /dev/null +++ b/work/Easy/Hash Table/242.valid-anagram.js @@ -0,0 +1,55 @@ +/* + * 242. Valid Anagram + * Difficulty: Easy + * https://leetcode.com/problems/valid-anagram/ + * + * ────────────────────────────────────────────────── + * + * Given two strings s and t, return true if t is an anagram of s, and + * false otherwise. + * + * + * + * Example 1: + * + * Input: s = "anagram", t = "nagaram" + * + * Output: true + * + * Example 2: + * + * Input: s = "rat", t = "car" + * + * Output: false + * + * + * + * Constraints: + * + * • 1 <= s.length, t.length <= 5 * 10^4 + * + * • s and t consist of lowercase English letters. + * + * + * + * Follow up: What if the inputs contain Unicode characters? How would + * you adapt your solution to such a case? +*/ + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + let freq = {}; + for (let c of s) freq[c] = (freq[c] || 0) + 1; + + for (let c of t) { + if(!freq[c] || freq[c] === 0){ + return false; + } + freq[c] = freq[c] - 1; + } + return true; +}; diff --git a/work/Medium/Array/189.rotate-array.js b/work/Medium/Array/189.rotate-array.js new file mode 100644 index 0000000..c529823 --- /dev/null +++ b/work/Medium/Array/189.rotate-array.js @@ -0,0 +1,57 @@ +/* + * 189. Rotate Array + * Difficulty: Medium + * https://leetcode.com/problems/rotate-array/ + * + * ────────────────────────────────────────────────── + * + * Given an integer array nums, rotate the array to the right by k + * steps, where k is non-negative. + * + * + * + * Example 1: + * + * Input: nums = [1,2,3,4,5,6,7], k = 3 + * Output: [5,6,7,1,2,3,4] + * Explanation: + * rotate 1 steps to the right: [7,1,2,3,4,5,6] + * rotate 2 steps to the right: [6,7,1,2,3,4,5] + * rotate 3 steps to the right: [5,6,7,1,2,3,4] + * + * Example 2: + * + * Input: nums = [-1,-100,3,99], k = 2 + * Output: [3,99,-1,-100] + * Explanation: + * rotate 1 steps to the right: [99,-1,-100,3] + * rotate 2 steps to the right: [3,99,-1,-100] + * + * + * + * Constraints: + * + * • 1 <= nums.length <= 10^5 + * + * • -2^31 <= nums[i] <= 2^31 - 1 + * + * • 0 <= k <= 10^5 + * + * + * + * Follow up: + * + * • Try to come up with as many solutions as you can. There are at + * least three different ways to solve this problem. + * + * • Could you do it in-place with O(1) extra space? +*/ + +/** + * @param {number[]} nums + * @param {number} k + * @return {void} Do not return anything, modify nums in-place instead. + */ +var rotate = function(nums, k) { + +}; diff --git a/work/Medium/Array/49.group-anagrams.js b/work/Medium/Array/49.group-anagrams.js new file mode 100644 index 0000000..3b18fd9 --- /dev/null +++ b/work/Medium/Array/49.group-anagrams.js @@ -0,0 +1,69 @@ +/* + * 49. Group Anagrams + * Difficulty: Medium + * https://leetcode.com/problems/group-anagrams/ + * + * ────────────────────────────────────────────────── + * + * Given an array of strings strs, group the anagrams together. You can + * return the answer in any order. + * + * + * + * Example 1: + * + * Input: strs = ["eat","tea","tan","ate","nat","bat"] + * + * Output: [["bat"],["nat","tan"],["ate","eat","tea"]] + * + * Explanation: + * + * • There is no string in strs that can be rearranged to form "bat". + * + * • The strings "nat" and "tan" are anagrams as they can be rearranged + * to form each other. + * + * • The strings "ate", "eat", and "tea" are anagrams as they can be + * rearranged to form each other. + * + * Example 2: + * + * Input: strs = [""] + * + * Output: [[""]] + * + * Example 3: + * + * Input: strs = ["a"] + * + * Output: [["a"]] + * + * + * + * Constraints: + * + * • 1 <= strs.length <= 10^4 + * + * • 0 <= strs[i].length <= 100 + * + * • strs[i] consists of lowercase English letters. +*/ + +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function(strs) { + let sorted = strs.map(str => str.split("").sort().join("")); + let anagrams = {}; + + for (let i = 0; i < strs.length; i++){ + if(!anagrams[sorted[i]]){ + anagrams[sorted[i]] = [strs[i]] + }else{ + anagrams[sorted[i]].push(strs[i]) + } + } + + return Object.values(anagrams); +};