Skip to content

Instantly share code, notes, and snippets.

@TangoPJ
Last active December 3, 2022 19:13
Show Gist options
  • Save TangoPJ/192cee1d52ebfc3e81fca5a9ef8ff33f to your computer and use it in GitHub Desktop.
Save TangoPJ/192cee1d52ebfc3e81fca5a9ef8ff33f to your computer and use it in GitHub Desktop.
/**
* @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