LeetCode 0014 Longest Common Prefix
2022.07.18
問題
typescript
typescriptfunction longestCommonPrefix(strs: string[]): string { if (strs.length === 0) return '' if (strs.length === 1) return strs[0] let prefix = '' let cnt = 0 while ( strs.every(str => str.startsWith(prefix)) ) { cnt += 1 if (strs[0].length < cnt) break prefix = strs[0].slice(0, cnt) } if (cnt-1 === 0) return '' return strs[0].slice(0, cnt-1) };