
Given an array of strings strs, find and return the longest common prefix — the longest string that appears as a leading substring in every string of the array.
If the strings share no common starting characters, return an empty string "".
function longestCommonPrefix(strs: string[]): string {
strs.sort()
const first = strs[0] ?? ""
const last = strs[strs.length - 1] ?? ""
const minStringLength = Math.min(first.length, last.length)
let i = 0
let common = ""
while (first.charAt(i) === last.charAt(i) && i < minStringLength) {
common = common + first.charAt(i)
i++
}
return common
}
console.log(longestCommonPrefix(["flower","flow","flight"]))
console.log(longestCommonPrefix(["dog","racecar","car"]))