Skip to content

Commit

Permalink
140th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 9, 2024
1 parent d82d476 commit 7196bca
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Ace Coding Interview with 75 Qs
| 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 |
| 151. Reverse Words in a String | [Solution][151] | Medium |
| 238. Product of Array Except Self | Solution | Medium |
| 334. Increasing Triplet Subsequence | Solution | Medium |
| 443. String Compression | Solution | Medium |
Expand All @@ -76,6 +76,7 @@ Ace Coding Interview with 75 Qs
[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
[151]: ./src/page-2/151.%20Reverse%20Words%20in%20a%20String/reverseWords.ts

| Two Pointers | | |
| ------------------------------- | --------------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gcdOfStrings } from './gcdOfStrings';

describe('1071. Greatest Common Divisor of Strings', () => {
it('gcdOfStrings', () => {
test('gcdOfStrings', () => {
expect(gcdOfStrings('ABCABC', 'ABC')).toBe('ABC');
expect(gcdOfStrings('ABABAB', 'ABAB')).toBe('AB');
expect(gcdOfStrings('LEET', 'CODE')).toBe('');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { kidsWithCandies } from './kidsWithCandies';

describe('1431. Kids With the Greatest Number of Candies', () => {
it('kidsWithCandies', () => {
test('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]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { mergeAlternately, mergeAlternately2 } from './mergeAlternately';

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

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

describe('151. Reverse Words in a String', () => {
test('reverseWords', () => {
expect(reverseWords('the sky is blue')).toBe('blue is sky the');
expect(reverseWords(' hello world ')).toBe('world hello');
expect(reverseWords('a good example')).toBe('example good a');
});
});
18 changes: 18 additions & 0 deletions src/page-2/151. Reverse Words in a String/reverseWords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type ReverseWords = (s: string) => string;

/**
* Accepted
*/
export const reverseWords: ReverseWords = (s) => {
// Trim leading and trailing spaces
const trimmed = s.trim();

// Split the string by spaces to get an array of words
const words = trimmed.split(/\s+/).filter((word) => word.length > 0);

// Reverse the array of words
const reversedWords = words.reverse();

// Join the reversed array into a string with a single space separator
return reversedWords.join(' ');
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { canPlaceFlowers } from './canPlaceFlowers';

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

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

0 comments on commit 7196bca

Please sign in to comment.