We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 237b640 commit b7c9b99Copy full SHA for b7c9b99
1002-find-common-characters.js
@@ -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
36
+}
0 commit comments