Leetcode 14 - Longest prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

1
2
Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

1
2
3
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
let res = "";
if (strs.length == 0) return res;
let _first = strs[0].split('');
strs.shift();
for (let i = 0; i < _first.length; i++) {
let e = _first[i];
let temp = strs.filter(x => x[i] == e);
if (temp.length == strs.length) {
res += e;
} else {
break;
}
}
return res;
};