-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestCommonPrefix.js
38 lines (31 loc) · 1012 Bytes
/
longestCommonPrefix.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
38
function longestCommonPrefix(strs) {
// let size = strs.length;
// /* if size is 0, return empty string */
// if (size == 0)
// return "";
// if (size == 1)
// return strs[0];
// /* sort the array of strings */
// strs.sort();
// /* find the minimum length from first and last string */
// let end = Math.min(strs[0].length, strs[size-1].length);
// /* find the common prefix between the first and
// last string */
// let i = 0;
// while (i < end && strs[0][i] == strs[size-1][i] )
// i++;
// let pre = strs[0].substring(0, i);
// return pre;
let result=strs[0]
let length=result.length
for (let i=0; i<strs.length; i++){
while(strs[i].indexOf(result)!==0){
result=result.substring(0,length-1)
length--
}
if (result===""){
return ""
}
}
return result
}