Skip to content

Commit

Permalink
138th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 9, 2024
1 parent d8efba9 commit bcde19d
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 40 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ Ace Coding Interview with 75 Qs
| ---------------------------------------------- | ---------------- | ------ |
| 1768. Merge Strings Alternately | [Solution][1768] | Easy |
| 1071. Greatest Common Divisor of Strings | [Solution][1071] | Easy |
| 1431. Kids With the Greatest Number of Candies | Solution | Easy |
| 605. Can Place Flowers | Solution | Easy |
| 345. Reverse Vowels of a String | Solution | Easy |
| 1431. Kids With the Greatest Number of Candies | [Solution][1431] | Easy |
| 605. Can Place Flowers | [Solution][605] | Easy |
| 345. Reverse Vowels of a String | [Solution][345] | Easy |
| 151. Reverse Words in a String | Solution | Medium |
| 238. Product of Array Except Self | Solution | Medium |
| 334. Increasing Triplet Subsequence | Solution | Medium |
| 443. String Compression | Solution | Medium |

[1768]: ./src/page-17/1768.%20Merge%20Strings%20Alternately/merge-alternately.ts
[1768]: ./src/page-17/1768.%20Merge%20Strings%20Alternately/mergeAlternately.ts
[1071]: ./src/page-11/1071.%20Greatest%20Common%20Divisor%20of%20Strings/gcdOfStrings.ts
[1431]: ./src/page-14/1431.%20Kids%20With%20the%20Greatest%20Number%20of%20Candies/kidsWithCandies.ts
[605]: ./src/page-6/605.%20Can%20Place%20Flowers/canPlaceFlowers.ts
[345]: ./src/page-4/345.%20Reverse%20Vowels%20of%20a%20String/reverseVowels.ts

| Two Pointers | | |
| ------------------------------- | --------------- | ------ |
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { kidsWithCandies } from './kidsWithCandies';

describe('1431. Kids With the Greatest Number of Candies', () => {
it('kidsWithCandies', () => {
expect(kidsWithCandies([2, 3, 5, 1, 3], 3)).toStrictEqual([true, true, true, false, true]);
expect(kidsWithCandies([4, 2, 1, 1, 2], 1)).toStrictEqual([true, false, false, false, false]);
expect(kidsWithCandies([12, 1, 12], 10)).toStrictEqual([true, false, true]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type KidsWithCandies = (candies: number[], extraCandies: number) => boolean[];

/**
* Accepted
*/
export const kidsWithCandies: KidsWithCandies = (candies, extraCandies) => {
const maxCandies = Math.max(...candies);
return candies.map((candy) => candy + extraCandies >= maxCandies);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { mergeAlternately, mergeAlternately2 } from './mergeAlternately';

describe('1768. Merge Strings Alternately', () => {
it('mergeAlternately', () => {
expect(mergeAlternately('abc', 'pqr')).toBe('apbqcr');
expect(mergeAlternately('ab', 'pqrs')).toBe('apbqrs');
expect(mergeAlternately('abcd', 'pq')).toBe('apbqcd');
});

it('mergeAlternately2', () => {
expect(mergeAlternately2('abc', 'pqr')).toBe('apbqcr');
expect(mergeAlternately2('ab', 'pqrs')).toBe('apbqrs');
expect(mergeAlternately2('abcd', 'pq')).toBe('apbqcd');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { reverseVowels } from './reverseVowels';

describe('345. Reverse Vowels of a String', () => {
it('reverseVowels', () => {
expect(reverseVowels('hello')).toBe('holle');
expect(reverseVowels('leetcode')).toBe('leotcede');
});
});
36 changes: 36 additions & 0 deletions src/page-4/345. Reverse Vowels of a String/reverseVowels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
type ReverseVowels = (s: string) => string;

export const reverseVowels: ReverseVowels = (s) => {
// Helper function to check if a character is a vowel
function isVowel(char: string): boolean {
return 'aeiouAEIOU'.includes(char);
}

// Convert the string to an array to allow mutable operations
const chars = s.split('');

let [left, right] = [0, chars.length - 1];

// Use two pointers to find vowels from both ends of the string
while (left < right) {
// Move the left pointer to the next vowel
while (left < right && !isVowel(chars[left])) {
left += 1;
}

// Move the right pointer to the previous vowel
while (left < right && !isVowel(chars[right])) {
right -= 1;
}

// Swap the vowels
if (left < right) {
[chars[left], chars[right]] = [chars[right], chars[left]];
left += 1;
right -= 1;
}
}

// Convert the array back to a string and return it
return chars.join('');
};
8 changes: 8 additions & 0 deletions src/page-6/605. Can Place Flowers/canPlaceFlowers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { canPlaceFlowers } from './canPlaceFlowers';

describe('605. Can Place Flowers', () => {
it('canPlaceFlowers', () => {
expect(canPlaceFlowers([1, 0, 0, 0, 1], 1)).toBe(true);
expect(canPlaceFlowers([1, 0, 0, 0, 1], 2)).toBe(false);
});
});
48 changes: 48 additions & 0 deletions src/page-6/605. Can Place Flowers/canPlaceFlowers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
type CanPlaceFlowers = (flowerbed: number[], n: number) => boolean;

export const canPlaceFlowers: CanPlaceFlowers = (flowerbed, n) => {
// Counter to track the number of flowers planted
let count = 0;

// Current index in the flowerbed array
let i = 0;

// Length of the flowerbed array
const length = flowerbed.length;

// Traverse the flowerbed array
while (i < length) {
// Check if the current position is empty (0)
if (flowerbed[i] === 0) {
// Check if the previous position is empty or out of bounds (left boundary)
const prevEmpty = i === 0 || flowerbed[i - 1] === 0;

// Check if the next position is empty or out of bounds (right boundary)
const nextEmpty = i === length - 1 || flowerbed[i + 1] === 0;

// If both previous and next positions are empty (or out of bounds), we can plant a flower here
if (prevEmpty && nextEmpty) {
// Place a flower at the current position
flowerbed[i] = 1;

// Increment the flower counter
count += 1;

// If we have planted the required number of flowers, return true
if (count >= n) return true;

// Move to the position after the next one to avoid adjacent planting
i += 2;
} else {
// Move to the next position if planting is not possible here
i += 1;
}
} else {
// Skip the next position if the current one is already occupied
i += 2;
}
}

// After traversing the entire flowerbed, check if we have planted enough flowers
return count >= n;
};

0 comments on commit bcde19d

Please sign in to comment.