-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ38.js
37 lines (26 loc) · 850 Bytes
/
Q38.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 14. Longest Common 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 "".
var longestCommonPrefix = function(strs) {
let length = [];
let prefix = ""
let min = 0;
for(let i = 0; i<strs.length; i++) {
length[i] = strs[i].length
}
min = length.indexOf(Math.min(...length))
min = strs[min]
for(let i = 0; i< min.length; i++) {
for(let j = 0; j<strs.length; j++) {
for (let k = 0; k<strs[j].length; k++){
if (min[k] === strs[j][k]) {
prefix = prefix.concat(min[k])
}
}
}
}
strs = strs.sort()
return prefix
};
let strs = ["flower","flow","flight"]
console.log(longestCommonPrefix(strs));