1- // Array & Hashmap
1+ class ArrayAndHashmap{
2+ containsDuplicate = function(nums) {
3+ const map = new Map()
4+ for (let char of nums){
5+ map.set(char, (map.get(char)||0)+1)
6+ }
7+ for (let [key, value] of map){
8+ if (value>1)return true
9+ }
10+ return false;
11+ };
12+
13+ isAnagram = function(s, t) {
14+ if (s.length !== t.length)return false
15+ let map = new Map()
16+ for (let char of s){
17+ map.set(char, (map.get(char)||0)+1);
18+ }//you have map now compare with t
19+ for (let char of t){
20+ if (!map.has(char))return false
21+ map.set(char, map.get(char)-1);
22+ if (map.get(char)===0)map.delete(char)
23+ }
24+ return map.size===0
25+ };
226
3- var twoSum = function (nums, target) { // Find two numbers which are two-sum of target in nums
27+ twoSum = function (nums, target) { // Find two numbers which are two-sum of target in nums
428 const map = new Map(); // create a new map
529 for (let i = 0; i < nums.length; i++) { // now iterate over nums
630 const complement = target - nums[i] // find complement of target to current
@@ -9,68 +33,168 @@ var twoSum = function (nums, target) { // Find two numbers which are two-sum of
933 }
1034 map.set(nums[i], i) // map current number to current index otherwise
1135 }
12- };
36+ };
1337
14- class EncodeDecode { // Encode and decode string
15- encode(strs) {
16- let encoded = ''
17- for(let str of strs){
18- encoded += str.length + "#" + str
38+ groupAnagrams = function(strs) {
39+ const map = new Map()
40+ for (let str of strs){
41+ const sorted = str.split('').sort().join('')
42+ if (!map.has(sorted)) map.set(sorted, [])
43+ map.get(sorted).push(str)
1944 }
20- return encoded;
21- }
22- decode(str) {
23- let decoded = []
24- let i = 0;
25- while ( i < str.length){ //while i is in bound
26- let j = str.indexOf('#', i) //find delimeter
27- let length = parseInt(str.slice(i,j)); // find length
28- i = j+1; // start from the beginning of string
29- decoded.push(str.slice(i,i+length)) // push into str
30- i += length // jump to next string
31- }
32- return decoded
45+ return Array.from(map.values())
46+ };
47+
48+ topKFrequent = function (nums, k) {
49+ const frequencyMap = new Map()
50+ const buckets = Array.from({ // bucket sort
51+ length: nums.length + 1
52+ }, () => [])
53+ const result = []
54+ for (const num of nums) {
55+ frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1)
56+ }
57+ for (const [num, freq] of frequencyMap.entries()) {
58+ buckets[freq].push(num)
59+ }
60+ for (let i = buckets.length-1; i >= 0; i--) {
61+ for (const num of buckets[i]) {
62+ result.push(num)
63+ if (result.length === k) {
64+ return result
65+ }
66+ }
67+ }
68+ return result
3369 }
34- }
35-
36- function productExceptSelf(nums){
37- const n = nums.length;
38- const result = Array(n).fill(1)
39- let leftProduct = 1;
40- for (let i = 0; i<n; i++){
41- result[i] = leftProduct;
42- leftProduct *= nums[i]
70+
71+ EncodeDecode = function() { // Encode and decode string
72+ function encode(strs) {
73+ let encoded = ''
74+ for (let str of strs) {
75+ encoded += str.length + "#" + str
76+ }
77+ return encoded;
78+ }
79+ function decode(str) {
80+ let decoded = []
81+ let i = 0;
82+ while (i < str.length) { //while i is in bound
83+ let j = str.indexOf('#', i) //find delimeter
84+ let length = parseInt(str.slice(i, j)); // find length
85+ i = j + 1; // start from the beginning of string
86+ decoded.push(str.slice(i, i + length)) // push into str
87+ i += length // jump to next string
88+ }
89+ return decoded
90+ }
4391 }
44- let rightProduct = 1
45- for (let i=n-1; i>=0; i--){
46- result[i] *= rightProduct;
47- rightProduct *= nums[i]
92+
93+ productExceptSelf(nums) {
94+ const n = nums.length;
95+ const result = Array(n).fill(1)
96+ let leftProduct = 1;
97+ for (let i = 0; i < n; i++) {
98+ result[i] = leftProduct;
99+ leftProduct *= nums[i]
100+ }
101+ let rightProduct = 1
102+ for (let i = n - 1; i >= 0; i--) {
103+ result[i] *= rightProduct;
104+ rightProduct *= nums[i]
105+ }
106+ return result
48107 }
49- return result
108+
109+ longestConsecutive = function (nums) { // longest coonsecutive subsequence
110+ if (nums.length === 0) return 0
111+ let longest = 0;
112+ let numSet = new Set(nums)
113+ for (let num of nums) {
114+ if (!numSet.has(num - 1)) {
115+ let currentStreak = 1;
116+ let currentNum = num
117+ while (numSet.has(currentNum + 1)) {
118+ currentNum++;
119+ currentStreak++;
120+ }
121+ longest = Math.max(longest, currentStreak)
122+ }
123+ }
124+ return longest
125+ };
50126}
51127
52- var longestConsecutive = function(nums) { // longest coonsecutive subsequence
53- if (nums.length === 0) return 0
54- let longest = 0;
55- let numSet = new Set(nums)
56- for (let num of nums){
57- if (!numSet.has(num-1)){
58- let currentStreak = 1;
59- let currentNum = num
60- while ( numSet.has(currentNum+1) ){
61- currentNum++;
62- currentStreak++;
128+ class Twopointer{
129+ isPalindrome = function (s) { // Check if a string is a palindrome
130+ const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, '');
131+ return cleaned === cleaned.split('').reverse().join('');
132+ };
133+
134+ threeSum = function (nums) {
135+ const result = []; // result
136+ nums.sort((a, b) => a - b) // sort the array
137+ for (let i = 0; i < nums.length - 2; i++) { // iterate over nums
138+ if (i > 0 && nums[i] === nums[i - 1]) continue // skip first duplicate
139+ let left = i + 1,
140+ right = nums.length - 1; // two pointer
141+ while (left < right) { // inner loop
142+ const currentSum = nums[i] + nums[left] + nums[right] // find sum
143+ if (currentSum === 0) { // if valid
144+ result.push([nums[i], nums[left], nums[right]]) //push into result
145+ while (left < right && nums[left] === nums[left + 1]) left++; //duplicates
146+ while (left < right && nums[right] === nums[right - 1]) right--;
147+ left++; // carry on
148+ right--;
149+ } else if (currentSum < 0) {
150+ left++
151+ } else right--;
63152 }
64- longest = Math.max(longest, currentStreak)
65153 }
66- }
67- return longest
68- };
69-
70- // Two pointer
154+ return result
155+ };
71156
72- var isPalindrome = function(s) { // Check is if a string is a palindrome
73- const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g,'');
74- return cleaned === cleaned.split('').reverse().join('');
75- };
157+ maxArea = function (height) { // container with most water
158+ let left = 0; // two pointers
159+ let right = height.length - 1;
160+ let maxWater = 0;
161+ while (left < right) {
162+ const area = Math.min(height[left], height[right]) * (right - left) // calculate area enclosed by left and right pillar
163+ maxWater = Math.max(maxWater, area) // if current area is maximum, update it
164+ if (height[left] < height[right]) { // continue with next larger pillar whether it is left or right
165+ left++;
166+ } else {
167+ right--;
168+ }
169+ }
170+ return maxWater;
171+ };
172+ }
76173
174+ class SlidingWindow{
175+ maxProfit = function (prices) { // Best time to buy and sell stocks
176+ let maxProfit = 0; // no profit
177+ let minPrice = Infinity; // highest price
178+ for (let price=0; price < prices.length; price++) { // look for every price
179+ minPrice = Math.min(minPrice, price); // compare cuurent price with minimum price
180+ const profit = price - minPrice; // find current profit
181+ maxProfit = Math.max(maxProfit, profit); // compare current profit with maximum profit
182+ }
183+ return maxProfit;
184+ };
185+
186+ lengthOfLongestSubstring = function (s) {
187+ let left = 0;
188+ let max_length = 0;
189+ const map = new Map();
190+ for (let right = 0; right < s.length; right++) {
191+ while (map.has(s[right])) {
192+ map.delete(s[left]);
193+ left++
194+ }
195+ map.set(s[right], right)
196+ max_length = Math.max(max_length, right - left + 1)
197+ }
198+ return max_length;
199+ };
200+ }
0 commit comments