Skip to content

Commit b7c9b99

Browse files
authored
Create 1002-find-common-characters.js
1 parent 237b640 commit b7c9b99

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

1002-find-common-characters.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string[]} A
3+
* @return {string[]}
4+
*/
5+
const commonChars = function(A) {
6+
const minArr = minEl(A)
7+
const res = []
8+
for(let i = 0; i < minArr[1]; i++) {
9+
let target = A[minArr[0]][i]
10+
let all = true
11+
for(let j = 0; j < A.length; j++) {
12+
if(j === minArr[0]) continue
13+
if(all === false) continue
14+
let idx
15+
if( (idx = A[j].indexOf(target)) === -1) {
16+
all = false
17+
} else {
18+
A[j] = A[j].slice(0, idx) + A[j].slice(idx + 1)
19+
}
20+
}
21+
if(all) res.push(target)
22+
}
23+
24+
return res
25+
};
26+
27+
function minEl(arr) {
28+
const res = [0, Number.MAX_SAFE_INTEGER] // [idx, len]
29+
for(let i = 0; i < arr.length; i++) {
30+
if(arr[i].length < res[1]) {
31+
res[0] = i
32+
res[1] = arr[i].length
33+
}
34+
}
35+
return res
36+
}

0 commit comments

Comments
 (0)