-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71bf639
commit 2d2b41f
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |