Created
April 16, 2024 00:39
-
-
Save yevmoroz/7399ba5799300bd6ef413cbed1e08615 to your computer and use it in GitHub Desktop.
leetcode longest prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string[]} strs | |
* @return {string} | |
*/ | |
function longestCommonPrefix(strs) { | |
if (strs.length === 0) { | |
return ""; | |
} | |
let prefix = strs[0]; | |
for (let word of strs) { | |
while (word.indexOf(prefix) !== 0) { | |
prefix = prefix.slice(0, -1); | |
if (prefix === '') { | |
return ''; | |
} | |
} | |
} | |
return prefix; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment