From 7196bcadde3ba2fdad9a2dbbff5221125eb624a7 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 9 Jun 2024 18:50:55 +0800 Subject: [PATCH] 140th Commit --- README.md | 3 ++- ...dOfStrings.spec.ts => gcdOfStrings.test.ts} | 2 +- ...Candies.spec.ts => kidsWithCandies.test.ts} | 2 +- ...nately.spec.ts => mergeAlternately.test.ts} | 4 ++-- .../reverseWords.test.ts | 9 +++++++++ .../reverseWords.ts | 18 ++++++++++++++++++ ...Flowers.spec.ts => canPlaceFlowers.test.ts} | 2 +- .../605. Can Place Flowers/canPlaceFlowers.ts | 3 +++ 8 files changed, 37 insertions(+), 6 deletions(-) rename src/page-11/1071. Greatest Common Divisor of Strings/{gcdOfStrings.spec.ts => gcdOfStrings.test.ts} (90%) rename src/page-14/1431. Kids With the Greatest Number of Candies/{kidsWithCandies.spec.ts => kidsWithCandies.test.ts} (92%) rename src/page-17/1768. Merge Strings Alternately/{mergeAlternately.spec.ts => mergeAlternately.test.ts} (87%) create mode 100644 src/page-2/151. Reverse Words in a String/reverseWords.test.ts create mode 100644 src/page-2/151. Reverse Words in a String/reverseWords.ts rename src/page-6/605. Can Place Flowers/{canPlaceFlowers.spec.ts => canPlaceFlowers.test.ts} (87%) diff --git a/README.md b/README.md index bdd76da..1db5232 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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 | | | | ------------------------------- | --------------- | ------ | diff --git a/src/page-11/1071. Greatest Common Divisor of Strings/gcdOfStrings.spec.ts b/src/page-11/1071. Greatest Common Divisor of Strings/gcdOfStrings.test.ts similarity index 90% rename from src/page-11/1071. Greatest Common Divisor of Strings/gcdOfStrings.spec.ts rename to src/page-11/1071. Greatest Common Divisor of Strings/gcdOfStrings.test.ts index 4d3f03b..b4968d5 100644 --- a/src/page-11/1071. Greatest Common Divisor of Strings/gcdOfStrings.spec.ts +++ b/src/page-11/1071. Greatest Common Divisor of Strings/gcdOfStrings.test.ts @@ -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(''); diff --git a/src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.spec.ts b/src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.test.ts similarity index 92% rename from src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.spec.ts rename to src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.test.ts index afe7aa6..880d344 100644 --- a/src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.spec.ts +++ b/src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.test.ts @@ -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]); diff --git a/src/page-17/1768. Merge Strings Alternately/mergeAlternately.spec.ts b/src/page-17/1768. Merge Strings Alternately/mergeAlternately.test.ts similarity index 87% rename from src/page-17/1768. Merge Strings Alternately/mergeAlternately.spec.ts rename to src/page-17/1768. Merge Strings Alternately/mergeAlternately.test.ts index 2069235..38aab67 100644 --- a/src/page-17/1768. Merge Strings Alternately/mergeAlternately.spec.ts +++ b/src/page-17/1768. Merge Strings Alternately/mergeAlternately.test.ts @@ -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'); diff --git a/src/page-2/151. Reverse Words in a String/reverseWords.test.ts b/src/page-2/151. Reverse Words in a String/reverseWords.test.ts new file mode 100644 index 0000000..18ff123 --- /dev/null +++ b/src/page-2/151. Reverse Words in a String/reverseWords.test.ts @@ -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'); + }); +}); diff --git a/src/page-2/151. Reverse Words in a String/reverseWords.ts b/src/page-2/151. Reverse Words in a String/reverseWords.ts new file mode 100644 index 0000000..711a56c --- /dev/null +++ b/src/page-2/151. Reverse Words in a String/reverseWords.ts @@ -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(' '); +}; diff --git a/src/page-6/605. Can Place Flowers/canPlaceFlowers.spec.ts b/src/page-6/605. Can Place Flowers/canPlaceFlowers.test.ts similarity index 87% rename from src/page-6/605. Can Place Flowers/canPlaceFlowers.spec.ts rename to src/page-6/605. Can Place Flowers/canPlaceFlowers.test.ts index 53b04de..0be717f 100644 --- a/src/page-6/605. Can Place Flowers/canPlaceFlowers.spec.ts +++ b/src/page-6/605. Can Place Flowers/canPlaceFlowers.test.ts @@ -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); }); diff --git a/src/page-6/605. Can Place Flowers/canPlaceFlowers.ts b/src/page-6/605. Can Place Flowers/canPlaceFlowers.ts index eb53abd..d27755b 100644 --- a/src/page-6/605. Can Place Flowers/canPlaceFlowers.ts +++ b/src/page-6/605. Can Place Flowers/canPlaceFlowers.ts @@ -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;