Skip to content

Commit ba6e748

Browse files
authored
Merge pull request neetcode-gh#1320 from delsint123/delsint123-patch-3
Create 14-Longest-Common-Prefix.js
2 parents 22c8b38 + 6e3ddb3 commit ba6e748

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Time O(N^2) | Space O(N)
3+
* @param {string[]} strs
4+
* @return {string}
5+
*/
6+
var longestCommonPrefix = function(strs) {
7+
8+
let pre = strs[0];
9+
10+
for(let word of strs) {
11+
12+
for(let i = pre.length - 1; i >= 0; i--) {
13+
14+
if(pre[i] !== word[i]) {
15+
pre = pre.slice(0, i);
16+
}
17+
}
18+
}
19+
20+
return pre;
21+
};

0 commit comments

Comments
 (0)