1
- class ArrayAndHashmap{
2
- containsDuplicate = function(nums) {
1
+ class ArrayAndHashmap {
2
+ containsDuplicate = function (nums) {
3
3
const map = new Map()
4
- for (let char of nums){
5
- map.set(char, (map.get(char)||0)+ 1)
4
+ for (let char of nums) {
5
+ map.set(char, (map.get(char) || 0) + 1)
6
6
}
7
- for (let [key, value] of map){
8
- if (value>1) return true
7
+ for (let [key, value] of map) {
8
+ if (value > 1) return true
9
9
}
10
10
return false;
11
11
};
12
12
13
- isAnagram = function(s, t) {
14
- if (s.length !== t.length)return false
13
+ isAnagram = function (s, t) {
14
+ if (s.length !== t.length) return false
15
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
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
25
};
26
26
27
27
twoSum = function (nums, target) { // Find two numbers which are two-sum of target in nums
28
- const map = new Map(); // create a new map
29
- for (let i = 0; i < nums.length; i++) { // now iterate over nums
30
- const complement = target - nums[i] // find complement of target to current
31
- if (map.has(complement)) { // if there's complement in map
32
- return [map.get(complement), i] // return complement and current index
28
+ const map = new Map(); // create a new map
29
+ for (let i = 0; i < nums.length; i++) { // now iterate over nums
30
+ const complement = target - nums[i] // find complement of target to current
31
+ if (map.has(complement)) { // if there's complement in map
32
+ return [map.get(complement), i] // return complement and current index
33
+ }
34
+ map.set(nums[i], i) // map current number to current index otherwise
33
35
}
34
- map.set(nums[i], i) // map current number to current index otherwise
35
- }
36
36
};
37
37
38
- groupAnagrams = function(strs) {
38
+ groupAnagrams = function (strs) {
39
39
const map = new Map()
40
- for (let str of strs){
40
+ for (let str of strs) {
41
41
const sorted = str.split('').sort().join('')
42
42
if (!map.has(sorted)) map.set(sorted, [])
43
43
map.get(sorted).push(str)
44
44
}
45
45
return Array.from(map.values())
46
46
};
47
-
47
+
48
48
topKFrequent = function (nums, k) {
49
49
const frequencyMap = new Map()
50
50
const buckets = Array.from({ // bucket sort
@@ -57,7 +57,7 @@ class ArrayAndHashmap{
57
57
for (const [num, freq] of frequencyMap.entries()) {
58
58
buckets[freq].push(num)
59
59
}
60
- for (let i = buckets.length- 1; i >= 0; i--) {
60
+ for (let i = buckets.length - 1; i >= 0; i--) {
61
61
for (const num of buckets[i]) {
62
62
result.push(num)
63
63
if (result.length === k) {
@@ -67,15 +67,16 @@ class ArrayAndHashmap{
67
67
}
68
68
return result
69
69
}
70
-
71
- EncodeDecode = function() { // Encode and decode string
70
+
71
+ EncodeDecode = function () { // Encode and decode string
72
72
function encode(strs) {
73
73
let encoded = ''
74
74
for (let str of strs) {
75
75
encoded += str.length + "#" + str
76
76
}
77
77
return encoded;
78
78
}
79
+
79
80
function decode(str) {
80
81
let decoded = []
81
82
let i = 0;
@@ -89,7 +90,7 @@ class ArrayAndHashmap{
89
90
return decoded
90
91
}
91
92
}
92
-
93
+
93
94
productExceptSelf(nums) {
94
95
const n = nums.length;
95
96
const result = Array(n).fill(1)
@@ -105,7 +106,7 @@ class ArrayAndHashmap{
105
106
}
106
107
return result
107
108
}
108
-
109
+
109
110
longestConsecutive = function (nums) { // longest coonsecutive subsequence
110
111
if (nums.length === 0) return 0
111
112
let longest = 0;
@@ -125,12 +126,12 @@ class ArrayAndHashmap{
125
126
};
126
127
}
127
128
128
- class Twopointer{
129
+ class Twopointer {
129
130
isPalindrome = function (s) { // Check if a string is a palindrome
130
131
const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, '');
131
132
return cleaned === cleaned.split('').reverse().join('');
132
133
};
133
-
134
+
134
135
threeSum = function (nums) {
135
136
const result = []; // result
136
137
nums.sort((a, b) => a - b) // sort the array
@@ -171,19 +172,19 @@ class Twopointer{
171
172
};
172
173
}
173
174
174
- class SlidingWindow{
175
- maxProfit = function(prices) { // Best time to buy and sell stocks
175
+ class SlidingWindow {
176
+ maxProfit = function (prices) { // Best time to buy and sell stocks
176
177
let maxProfit = 0;
177
178
let minPrice = Infinity;
178
- for (let price of prices){
179
+ for (let price of prices) {
179
180
minPrice = Math.min(minPrice, price);
180
181
const profit = price - minPrice;
181
182
maxProfit = Math.max(maxProfit, profit);
182
183
}
183
184
return maxProfit;
184
185
};
185
-
186
- lengthOfLongestSubstring = function (s) {
186
+
187
+ lengthOfLongestSubstring = function (s) { // without repeating characters
187
188
let left = 0;
188
189
let max_length = 0;
189
190
const map = new Map();
@@ -196,5 +197,151 @@ class SlidingWindow{
196
197
max_length = Math.max(max_length, right - left + 1)
197
198
}
198
199
return max_length;
199
- };
200
+ };
201
+
202
+ characterReplacement = function(s, k) { //Longest repeating character replacement
203
+ let left = 0, freq = {}, maxFrequency = 0, maxLength = 0;
204
+ for (let right = 0; right < s.length; right++){
205
+ const char = s[right];
206
+ freq[char] = (freq[char] || 0)+1
207
+ maxFrequency = Math.max(maxFrequency, freq[char])
208
+ while ((right-left+1)-maxFrequency > k) {
209
+ freq[s[left]]--
210
+ left++
211
+ }
212
+ maxLength = Math.max(maxLength, right-left+1)
213
+ }
214
+ return maxLength
215
+ };
216
+
217
+ minWindow(s, t) { // minimum window substring
218
+ if (s.length < t.length) return "";
219
+
220
+ const requiredChars = new Map();
221
+ for (const char of t) {
222
+ requiredChars.set(char, (requiredChars.get(char) || 0) + 1);
223
+ }
224
+
225
+ let left = 0;
226
+ let right = 0;
227
+ let formed = 0;
228
+ const windowCounts = new Map();
229
+ const required = requiredChars.size;
230
+
231
+ let minLength = Infinity;
232
+ let minWindowStart = 0;
233
+
234
+ while (right < s.length) {
235
+ const char = s[right];
236
+ windowCounts.set(char, (windowCounts.get(char) || 0) + 1);
237
+
238
+ if (requiredChars.has(char) && windowCounts.get(char) === requiredChars.get(char)) {
239
+ formed += 1;
240
+ }
241
+
242
+ while (left <= right && formed === required) {
243
+ if (right - left + 1 < minLength) {
244
+ minLength = right - left + 1;
245
+ minWindowStart = left;
246
+ }
247
+
248
+ const leftChar = s[left];
249
+ windowCounts.set(leftChar, windowCounts.get(leftChar) - 1);
250
+
251
+ if (requiredChars.has(leftChar) && windowCounts.get(leftChar) < requiredChars.get(leftChar)) {
252
+ formed -= 1;
253
+ }
254
+ left += 1;
255
+ }
256
+ right += 1;
257
+ }
258
+
259
+ return minLength === Infinity ? "" : s.slice(minWindowStart, minWindowStart + minLength);
260
+ }
261
+ }
262
+
263
+ class Stack {
264
+ isValid(s) { // valid parantheses
265
+ const stack = [];
266
+ const map = {
267
+ ')': '(',
268
+ '}': '{',
269
+ ']': '['
270
+ };
271
+
272
+ for (let char of s) {
273
+ if (char === '(' || char === '{' || char === '[') { // Push opening brackets onto the stack
274
+ stack.push(char);
275
+ } else { // If the stack is empty or the top of the stack does not match the current closing bracket, return false
276
+ if (stack.length === 0 || stack.pop() !== map[char]) {
277
+ return false;
278
+ }
279
+ }
280
+ }
281
+
282
+ return stack.length === 0; // If the stack is empty at the end, all parentheses are closed properly
283
+ }
284
+ }
285
+
286
+ class BinarySearch{
287
+ findMin(nums) { // find minimum in rotated sorted array
288
+ let left = 0;
289
+ let right = nums.length - 1;
290
+
291
+ if (nums[left] < nums[right] || nums.length === 1) { // If the array is not rotated or has one element, return the first element
292
+ return nums[left];
293
+ }
294
+
295
+ while (left <= right) {
296
+ let mid = Math.floor((left + right) / 2);
297
+
298
+ if (nums[mid] > nums[mid + 1]) { // Check if mid element is the smallest
299
+ return nums[mid + 1];
300
+ }
301
+
302
+ if (nums[mid - 1] > nums[mid]) { // Check if the element before mid is the smallest
303
+ return nums[mid];
304
+ }
305
+
306
+ if (nums[mid] > nums[left]) { // Adjust search range
307
+ left = mid + 1;
308
+ } else {
309
+ right = mid - 1;
310
+ }
311
+ }
312
+ }
313
+
314
+ search(nums, target) { // search in rotated sorted array
315
+ let left = 0;
316
+ let right = nums.length - 1;
317
+
318
+ while (left <= right) {
319
+ let mid = Math.floor((left + right) / 2);
320
+
321
+ if (nums[mid] === target) { // Check if the target is found at the mid index
322
+ return mid;
323
+ }
324
+
325
+ if (nums[left] <= nums[mid]) { // If the left part is sorted
326
+ if (target >= nums[left] && target < nums[mid]) { // Check if target is in the left part
327
+ right = mid - 1; // Target is in the left part
328
+ } else {
329
+ left = mid + 1; // Target is in the right part
330
+ }
331
+ }
332
+ else { // If the right part is sorted
333
+ if (target > nums[mid] && target <= nums[right]) { // Check if target is in the right part
334
+ left = mid + 1; // Target is in the right part
335
+ } else {
336
+ right = mid - 1; // Target is in the left part
337
+ }
338
+ }
339
+ }
340
+
341
+ return -1; // Target not found
342
+ }
343
+ }
344
+
345
+ class LinkedList{
346
+
200
347
}
0 commit comments