function longestCommonPrefix(strs) {
if (!strs.length) return '';
let prefix = strs[0];
for (const s of strs) while (!s.startsWith(prefix)) prefix = prefix.slice(0, -1);
return prefix;
}
longestCommonPrefix(['flower', 'flow', 'flight']); // 'fl'Find the longest common prefix by progressively trimming the candidate string.
Find the longest common prefix among an array of strings. A prefix is a substring that appears at the start of multiple strings.
["flower", "flow", "flight"] β "fl"
["dog", "racecar", "car"] β ""
["interspecies", "interstellar", "interstate"] β "inters"
Algorithm:
function longestCommonPrefix(strs: string[]): string {
// Return the longest common prefix
}
longestCommonPrefix(['flower', 'flow', 'flight']) // "fl"