-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestCommonPrefix.js
More file actions
34 lines (32 loc) · 893 Bytes
/
longestCommonPrefix.js
File metadata and controls
34 lines (32 loc) · 893 Bytes
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
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix0 = function (strs) {
let pfix = strs[0];
let s, i, j;
for (i = 1; i < strs.length && pfix !== ""; i++) {
s = strs[i];
for (j = 0; j < s.length && j < pfix.length && s[j] === pfix[j]; j++);
pfix = pfix.substring(0, j);
}
return pfix;
};
//faster
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function (strs) {
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (!strs[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length - 1);
if (prefix === "") return prefix;
}
}
return prefix;
};
console.log("test", longestCommonPrefix(["flower", "flower", "flower"]));
console.log("test", longestCommonPrefix(["flower", "flow", "flight"]));
console.log("test", longestCommonPrefix(["dog", "racecar", "car"]));