Longest Common Prefix
约 179 字小于 1 分钟
2026-02-11
题目
Longest Common Prefix
Write a type, LongestCommonPrefix that returns the longest common prefix string amongst a tuple of strings.
If there is no common prefix, return an empty string "".
type Common = LongestCommonPrefix<["flower", "flow", "flight"]>
// ?^ "fl"
type Uncommon = LongestCommonPrefix<["dog", "racecar", "race"]>
// ?^ ""Inspired by LeetCode 14. Longest Common Prefix
解题思路
待补充
答案
type LongestCommonPrefix<T extends string[], P extends string = ''> = any验证
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<LongestCommonPrefix<['flower', 'flow', 'flight']>, 'fl'>>,
Expect<Equal<LongestCommonPrefix<['dog', 'racecar', 'race']>, ''>>,
Expect<Equal<LongestCommonPrefix<['', '', '']>, ''>>,
Expect<Equal<LongestCommonPrefix<['a', '', '']>, ''>>,
Expect<Equal<LongestCommonPrefix<['', 'a', '']>, ''>>,
Expect<Equal<LongestCommonPrefix<['', '', 'a']>, ''>>,
Expect<Equal<LongestCommonPrefix<['a', 'a', '']>, ''>>,
Expect<Equal<LongestCommonPrefix<['a', '', 'a']>, ''>>,
Expect<Equal<LongestCommonPrefix<['', 'a', 'a']>, ''>>,
Expect<Equal<LongestCommonPrefix<['a', 'a', 'a']>, 'a'>>,
Expect<Equal<LongestCommonPrefix<['abc', 'abcd', 'abcde']>, 'abc'>>,
Expect<Equal<LongestCommonPrefix<[' ', ' ', ' ']>, ' '>>,
Expect<Equal<LongestCommonPrefix<['type-challenges', 'type-hero', 'typescript']>, 'type'>>,
]参考
无
