Skip to content

Instantly share code, notes, and snippets.

@yevmoroz
Created April 16, 2024 00:39
Show Gist options
  • Save yevmoroz/7399ba5799300bd6ef413cbed1e08615 to your computer and use it in GitHub Desktop.
Save yevmoroz/7399ba5799300bd6ef413cbed1e08615 to your computer and use it in GitHub Desktop.
leetcode longest prefix
/**
* @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