Skip to content

Commit

Permalink
question: wordLengthProduct
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelriosoliveira committed Feb 19, 2024
1 parent 71bf639 commit 2d2b41f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ I started this in the [issue #176](https://buttondown.email/cassidoo/archive/we-
- [335 - flip](src/2024/335-flip)
- [337 - daysBetween](src/2024/337-daysBetween)
- [339 - fromTo](src/2024/339-fromTo)
- [340 - wordLengthProduct](src/2024/340-wordLengthProduct)
</details>

## Installing & Running
Expand Down
21 changes: 21 additions & 0 deletions src/2024/340-wordLengthProduct/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# wordLengthProduct

Interview question of the [issue #340 of rendezvous with cassidoo](https://buttondown.email/cassidoo/archive/dont-let-doubt-stop-you-from-getting-where-you/).

## The Question

**Given a string array, find the maximum product of word lengths where the words don't share any letters.**

Example:

```js
> wordLengthProduct(["fish","fear","boo","egg","cake","abcdef"])
> 16 // "fish" and "cake"

> wordLengthProduct(["a","aa","aaa","aaaa"])
> 0 // all of them share "a"
```

## Installing & Running

Just `pnpm i` to install all dependencies and then `pnpm t` to run the tests!
28 changes: 28 additions & 0 deletions src/2024/340-wordLengthProduct/wordLengthProduct.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { wordLengthProduct } from './wordLengthProduct';

describe('#wordLengthProduct', () => {
it('should return 16 for "fish" and "cake"', () => {
const result1 = wordLengthProduct(['fish', 'fear', 'boo', 'egg', 'cake', 'abcdef']);
expect(result1).toStrictEqual(16);
});

it('should return 16 for "fish" and "cake" in different positions', () => {
const result1 = wordLengthProduct(['fear', 'boo', 'egg', 'cake', 'abcdef', 'fish']);
expect(result1).toStrictEqual(16);
});

it('should return 0 for all words sharing the letter "a"', () => {
const result2 = wordLengthProduct(['havana', 'brazil', 'cuba', 'usa']);
expect(result2).toStrictEqual(0);
});

it('should return 0 for an empty array', () => {
const result = wordLengthProduct([]);
expect(result).toStrictEqual(0);
});

it('should return 0 for an array with one word', () => {
const result = wordLengthProduct(['hello']);
expect(result).toStrictEqual(0);
});
});
27 changes: 27 additions & 0 deletions src/2024/340-wordLengthProduct/wordLengthProduct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Given a string array, find the maximum product of word lengths where the words don't share any letters.
Example:
> wordLengthProduct(["fish","fear","boo","egg","cake","abcdef"])
> 16 // "fish" and "cake"
> wordLengthProduct(["a","aa","aaa","aaaa"])
> 0 // all of them share "a"
*/

export function wordLengthProduct(words: string[]): number {
return (
words
.map(word => ({
word,
pair: words
.filter(w => w.split('').every(char => !word.includes(char)))
.sort((a, b) => b.length - a.length)
.at(0),
}))
.map(({ word, pair }) => word.length * (pair?.length || 0))
.sort((a, b) => b - a)
.at(0) || 0
);
}

0 comments on commit 2d2b41f

Please sign in to comment.