Last active
December 3, 2022 19:13
-
-
Save TangoPJ/192cee1d52ebfc3e81fca5a9ef8ff33f to your computer and use it in GitHub Desktop.
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[]} strings | |
* @return {string} | |
*/ | |
const longestCommonPrefix = (strings) => { | |
if (strings.length === 1) { | |
return strings[0]; | |
} | |
const sortedStrings = strings.sort(); | |
const first = sortedStrings.at(0).split(''); | |
const last = sortedStrings.at(-1); | |
let result = ''; | |
first.every((letter, index) => { | |
if (letter === last[index]) { | |
return result += letter; | |
} | |
}); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment