Skip to content

Commit 44cb516

Browse files
committed
Solve LeetCode 75 problems 01-12
1 parent 55c44be commit 44cb516

File tree

50 files changed

+261
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+261
-0
lines changed

LeetCode-75/.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
// {
8+
// "type": "node",
9+
// "request": "launch",
10+
// "name": "Launch Program",
11+
// "skipFiles": [
12+
// "<node_internals>/**"
13+
// ],
14+
// "program": "${workspaceFolder}\\LeetCode-75\\11.js"
15+
// }
16+
]
17+
}

LeetCode-75/01-1768.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/greatest-common-divisor-of-strings/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const isStrValid = (str: string, substrLen: number): boolean => {
4+
const substrToCheck = str.substring(0, substrLen);
5+
return !str.split(substrToCheck).filter((val) => !!val).length;
6+
};
7+
8+
const gcdOfStrings = (str1: string, str2: string): string => {
9+
const maxStrLen = str1.length < str2.length ? str1.length : str2.length;
10+
let substrLen = maxStrLen;
11+
12+
while (substrLen > 0) {
13+
if (
14+
isStrValid(str1, substrLen) &&
15+
isStrValid(str2, substrLen) &&
16+
str1.startsWith(str2.substring(0, substrLen))
17+
)
18+
return str1.substring(0, substrLen);
19+
20+
substrLen--;
21+
}
22+
23+
return "";
24+
};

LeetCode-75/02-1071.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://leetcode.com/problems/merge-strings-alternately/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const mergeAlternately = (word1: string, word2: string): string => {
4+
const minLen = word1.length < word2.length ? word1.length : word2.length;
5+
6+
const word2Chars = word2.substring(0, minLen).split("");
7+
8+
return (
9+
word1
10+
.substring(0, minLen)
11+
.split("")
12+
.map((char, idx) => char + word2Chars[idx])
13+
.join("") +
14+
word1.substring(minLen) +
15+
word2.substring(minLen)
16+
);
17+
};

LeetCode-75/03-1431.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const kidsWithCandies = (
4+
candies: number[],
5+
extraCandies: number
6+
): boolean[] => {
7+
const biggestCandiesCountOwned = candies.reduce(
8+
(acc, val) => (val > acc ? val : acc),
9+
0
10+
);
11+
12+
return candies.map((el) => el + extraCandies >= biggestCandiesCountOwned);
13+
};

LeetCode-75/04-605.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/can-place-flowers/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const canPlaceFlowers = (flowerbed: number[], n: number): boolean => {
4+
let remaining = n,
5+
idx = 0;
6+
7+
while (remaining && idx < flowerbed.length) {
8+
if (
9+
!flowerbed[idx] &&
10+
((idx === 0 && !flowerbed[idx + 1]) ||
11+
(idx === flowerbed.length - 1 && !flowerbed[idx - 1]) ||
12+
(!flowerbed[idx - 1] && !flowerbed[idx + 1]))
13+
) {
14+
flowerbed[idx] = 1;
15+
remaining--;
16+
}
17+
18+
idx++;
19+
}
20+
21+
return !remaining;
22+
};

LeetCode-75/05-345.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/reverse-vowels-of-a-string/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const VOWELS = ["a", "e", "i", "o", "u"];
4+
5+
const reverseVowels = (str: string): string => {
6+
const savedVowels: string[] = [];
7+
const savedIdxs: number[] = [];
8+
const newStrArr = str.split("");
9+
str = str.toLowerCase();
10+
11+
for (let i = 0; i <= str.length; i++)
12+
if (VOWELS.includes(str[i])) {
13+
savedIdxs.push(i);
14+
savedVowels.push(newStrArr[i]);
15+
}
16+
17+
savedIdxs.forEach(
18+
(vowelIdx, idx) =>
19+
(newStrArr[vowelIdx] = savedVowels[savedIdxs.length - idx - 1])
20+
);
21+
22+
return newStrArr.join("");
23+
};

LeetCode-75/06-151.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// https://leetcode.com/problems/reverse-words-in-a-string/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const reverseWords = (str: string): string =>
4+
str
5+
.match(/[^\s]+/g)
6+
?.reverse()
7+
.join(" ") || "";

LeetCode-75/07-238.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/product-of-array-except-self/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const productExceptSelf = (nums: number[]): number[] => {
4+
// O(N^2) SOLUTION
5+
// return nums.map((_el, outerIdx) =>
6+
// nums.reduce((acc, val, idx) => (idx === outerIdx ? acc : acc * val), 1)
7+
// );
8+
9+
// O(N) SOLUTION
10+
const prefixes = new Array(nums.length).fill(1);
11+
const postfixes = new Array(nums.length).fill(1);
12+
13+
for (let i = 0, prefix = 1; i < nums.length; i++) {
14+
prefix *= nums[i];
15+
prefixes[i] = prefix;
16+
}
17+
18+
for (let i = nums.length - 1, postfix = 1; i >= 0; i--) {
19+
postfix *= nums[i];
20+
postfixes[i] = postfix;
21+
}
22+
23+
return nums.map((_el, i) => (prefixes[i - 1] ?? 1) * (postfixes[i + 1] ?? 1));
24+
};

LeetCode-75/08-334.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/increasing-triplet-subsequence/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const increasingTriplet = (nums: number[]): boolean => {
4+
// O(N^3) SOLUTION
5+
// if (nums.length < 3) return false;
6+
7+
// for (let i = 0; i < nums.length - 2; i++)
8+
// for (let j = i + 1; j < nums.length - 1; j++)
9+
// if (nums[j] > nums[i])
10+
// for (let k = j + 1; k < nums.length; k++)
11+
// if (nums[k] > nums[j]) return true;
12+
13+
// return false;
14+
15+
// O(N) SOLUTION
16+
for (
17+
let i = 0, firstMin = Number.MAX_VALUE, secondMin = Number.MAX_VALUE;
18+
i < nums.length;
19+
i++
20+
) {
21+
if (nums[i] <= firstMin) firstMin = nums[i];
22+
else if (nums[i] <= secondMin) secondMin = nums[i];
23+
else return true;
24+
}
25+
26+
return false;
27+
};

LeetCode-75/09-443.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/string-compression/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const compress = (chars: string[]): number => {
4+
if (!chars.length) return 0;
5+
6+
let compressed = "",
7+
currChar = chars[0],
8+
currCharCount = 1;
9+
10+
for (let i = 1; i < chars.length; i++) {
11+
if (chars[i] === currChar) currCharCount++;
12+
else {
13+
compressed =
14+
compressed + currChar + (currCharCount === 1 ? "" : currCharCount);
15+
currChar = chars[i];
16+
currCharCount = 1;
17+
}
18+
}
19+
20+
compressed =
21+
compressed + currChar + (currCharCount === 1 ? "" : currCharCount);
22+
23+
for (let i = 0; i < compressed.length; i++) chars[i] = compressed[i];
24+
25+
return compressed.length;
26+
};

0 commit comments

Comments
 (0)