Skip to content

Commit 027f65d

Browse files
committed
Add solution #1002
1 parent d75f4c7 commit 027f65d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
989|[Add to Array-Form of Integer](./0989-add-to-array-form-of-integer.js)|Easy|
175175
994|[Rotting Oranges](./0994-rotting-oranges.js)|Medium|
176176
997|[Find the Town Judge](./0997-find-the-town-judge.js)|Easy|
177+
1002|[Find Common Characters](./1002-find-common-characters.js)|Easy|
177178
1009|[Complement of Base 10 Integer](./1009-complement-of-base-10-integer.js)|Easy|
178179
1010|[Pairs of Songs With Total Durations Divisible by 60](./1010-pairs-of-songs-with-total-durations-divisible-by-60.js)|Medium|
179180
1022|[Sum of Root To Leaf Binary Numbers](./1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 1002. Find Common Characters
3+
* https://leetcode.com/problems/find-common-characters/
4+
* Difficulty: Easy
5+
*
6+
* Given a string array words, return an array of all characters that show up in all strings within
7+
* the words (including duplicates). You may return the answer in any order.
8+
*/
9+
10+
/**
11+
* @param {string[]} words
12+
* @return {string[]}
13+
*/
14+
var commonChars = function(words) {
15+
return words
16+
.map(word => [...word])
17+
.reduce((common, word) => common.filter(s => {
18+
const index = word.indexOf(s);
19+
if (index !== -1) {
20+
word.splice(index, 1);
21+
}
22+
return index !== -1;
23+
}), [...words[0]]);
24+
};

0 commit comments

Comments
 (0)