Skip to content

Commit 3587fb8

Browse files
committed
Time: 4 ms (23.75%), Space: 54.4 MB (59.44%) - LeetHub
source:c977e34621829bd5190556eaf1091ec8c34c28dd
1 parent e40e09a commit 3587fb8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string}
4+
*/
5+
var longestCommonPrefix = function(strs) {
6+
if (strs.length === 0) return "";
7+
8+
const base = strs[0];
9+
10+
for (let i = 0; i < base.length; i++) {
11+
const ch = base[i];
12+
for (let j = 1; j < strs.length; j++) {
13+
if (i >= strs[j].length || strs[j][i] !== ch) {
14+
return base.slice(0, i);
15+
}
16+
}
17+
}
18+
return base;
19+
};

0 commit comments

Comments
 (0)