Skip to content

Instantly share code, notes, and snippets.

@ms314006
Last active March 23, 2020 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms314006/a14c427de55e192259d584dd1664bc59 to your computer and use it in GitHub Desktop.
Save ms314006/a14c427de55e192259d584dd1664bc59 to your computer and use it in GitHub Desktop.
var longestCommonPrefix = function(strs) {
const firstStr = strs[0];
if (!firstStr) return '';
let condition = true;
let count = -1;
while(condition) {
count += 1;
if (count === firstStr.length) {
condition = false;
} else {
condition = strs.every(str => (
firstStr[count] === str[count]
));
}
}
return firstStr.slice(0, count);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment