type CheckRepeatedChars<T extends string, A extends any[] = []> = T extends `${infer F}${infer Rest}`
? F extends A[number]
? true
: CheckRepeatedChars<Rest, [...A, F]>
: false;
Solution by wendao-liu #35094
// your answers
type CheckRepeatedChars<T extends string, A extends string[] = []> = T extends `${infer F}${infer L}` ? F extends A[number] ? true : CheckRepeatedChars<L, [F, ...A]> : false
Solution by gobielJonathan #34536
// if T includes S, return true
type Includes<T extends string, S extends string> = T extends `${infer F}${infer R}`
? Equal<F, S> extends true
? true
: Includes<R, S>
: false;
// produce unique characters of type parameter S
type Unique<S extends string> = S extends `${infer F}${infer R}`
? Includes<R, F> extends true
? Unique<R>
: `${F}${Unique<R>}`
: "";
type StringToTuple<S extends string> = S extends `${infer F}${infer R}`
? [F, ...StringToTuple<R>]
: [];
type CheckRepeatedChars<T extends string> = StringToTuple<T>["length"] extends StringToTuple<
Unique<T>
>["length"]
? false
: true;
Solution by yukicountry #34347
type CheckRepeatedChars<T extends string> = T extends `${infer R}${infer rest}` ? (rest extends `${string}${R}${string}` ? true : CheckRepeatedChars<rest>) : false
Solution by ouzexi #34107
type UnionToIntersection<U> = (U extends U ? (k: U) => 0 : never) extends (k: infer I) => 0 ? I : never
type LastUnion<U> = UnionToIntersection<
(U extends U ? (k: U) => 0 : never)
> extends (k: infer I) => 0 ? I : never
type UnionToTuple<T> = [T] extends [never] ? [] : [...UnionToTuple<Exclude<T, LastUnion<T>>>, LastUnion<T>]
type UnionLength<T> = UnionToTuple<T>['length']
type CheckRepeatedChars<T extends string>
= StrToArr<T>['length'] extends UnionLength<StrToUnion<T>>
? false
: true
Solution by ScriptBloom #33960
type isEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends (<G>() => G extends U ? 1 : 2) ? true : false
type Includes<T extends any[], U> = T extends [infer First, ...infer Rest] ? isEqual<First, U> extends true ? true : Includes<Rest, U> : false
type CheckRepeatedChars<T extends string, Tuple extends any[] = []> = T extends `${infer First}${infer Rest}` ? CheckRepeatedChars<Rest, [...Tuple, First]>
: Tuple extends [infer F, ...infer R]
? Includes<R, F> extends true
? true
: CheckRepeatedChars<'', R>
: false
Solution by PiligrimStas #33796
first_char extends pre_chars_union
type CheckRepeatedChars<S extends string, U = ''> =
S extends `${infer Char}${infer Rest}`
? Char extends U ? true : CheckRepeatedChars<Rest, U | Char>
: false
Solution by sunupupup #33438
// your answers
type CheckRepeatedChars<T extends string> = T extends `${infer L}${infer R}`
? R extends `${infer _}${L}${infer _}`
? true : CheckRepeatedChars<R>
: false
Solution by pea-sys #33226
type StringToTuple<T extends string, Acc extends string[] = []> = T extends `${infer F}${infer R}`
? StringToTuple<R, [...Acc, F]>
: Acc
type CheckRepeatedChars<T extends string> = T extends `${infer F}${infer R}`
? F extends StringToTuple<R>[number]
? true
: CheckRepeatedChars<R>
: false
Solution by keyurparalkar #32651
type CheckRepeatedChars<
T extends string,
S extends string[] = []
> = T extends `${infer F}${infer R}`
? F extends S[number]
? true
: CheckRepeatedChars<R, [...S, F]>
: false;
Solution by vangie #32191
type StringToUnion<S extends string, V extends any[] = []> = S extends `${infer First}${infer Rest}`
? StringToUnion<Rest, [...V, First]>
: V[number];
type CheckRepeatedChars<T extends string> = T extends `${infer F}${infer Rest}`
? F extends StringToUnion<Rest>
? true
: CheckRepeatedChars<Rest>
: false;
Solution by leejaehyup #30862
type CheckRepeatedChars<T extends string, U extends any[] = []> = T extends `${infer A}${infer B}` ? A extends U[number] ? true : CheckRepeatedChars<B, [...U, A]> : false
Solution by dreamluo-plus #30682
将S转为Union,之后从Union中递归排除S,当S extends ''并且[Union] extends never时则为false反之为true
type StringToUnion<S extends string> = S extends `${infer L}${infer R}` ? L | StringToUnion<R> : never;
type CheckRepeatedChars<S extends string, U = StringToUnion<S>> =
[U] extends [never]
? S extends ''
? false : true
: S extends `${infer L}${infer R}`
? CheckRepeatedChars<R, Exclude<U, L>> : never
Solution by sv-98-maxin #30123
定义一个前缀数组,然后开始遍历字符串,检查每个字符是否存在于前缀数组中,存在即返回false,不存在则继续遍历。 当遍历完整个字符串,即返回true。
type CheckRepeatedChars<T extends string, Prefix extends string[]= []> =
T extends `${infer F}${infer R}`
? F extends Prefix[number]
? true
: CheckRepeatedChars<R, [...Prefix, F]>
: false
Solution by stevenaces #29421
type CheckRepeatedChars<
T extends string,
U extends string = ""
> = T extends `${infer F}${infer R}`
? F extends U
? true
: CheckRepeatedChars<R, U | F>
: false;
Solution by DoubleWoodLin #28841
来解新题了,好久没写了。
利用字符串解构,把 T 拆分成 前缀 F 和 后缀 E,按照从前往后的遍历,如果 后缀 E 中包含前缀 F 则表示存在相同字符,如果不包含则递归后缀 E
type CheckRepeatedChars<T extends string> = T extends `${infer F}${infer E}`
? E extends `${string}${F}${string}`
? true
: CheckRepeatedChars<E>
: false
Solution by linjunc #28150
type CheckRepeatedChars<T extends string> =
// 匹配第一个子字符X和后面所有的子字符Y
T extends `${infer X}${infer Y extends string}`
? // 通过字符Y去匹配是否存在子字符X,如果存在则有重复
Y extends `${infer A}${X}${infer B}`
? true
: CheckRepeatedChars<Y>
: false;
Solution by jiaowoxiaobala #27905
type StoTuple<T extends string> = T extends `${infer F}${infer R}`
? [F, ...StoTuple<R>]
: []
type C<T extends string[]> = T extends [infer F, ...infer R extends string[]]
? F extends R[number]
? true
: C<R>
: false
type CheckRepeatedChars<T extends string> = C<StoTuple<T>>
Solution by jazelly #27562
type CheckRepeatedChars<T extends string> =
T extends `${infer A}${infer Rest}`
? Rest extends `${infer _}${A}${infer _}`
? true
: CheckRepeatedChars<Rest>
: false
Solution by drylint #27399
type CheckRepeatedChars<
T extends string,
A extends string[] = []
> = T extends `${infer H}${infer R}`
? H extends A[number]
? true
: CheckRepeatedChars<R, [...A, H]>
: false
Solution by HydenLiu #27356
// 辅助函数 判断指定字符串是否重复
type IsRepeated<T extends string, U extends string, M extends any[] = []> =
T extends `${infer L}${infer R}` ?
U extends L ? IsRepeated<R, U, [...M, L]> : IsRepeated<R, U, M>
: M['length'] extends 1 ? false : true
// 结果
type CheckRepeatedChars<T extends string, O extends string = T> =
T extends `${infer L}${infer R}`
? IsRepeated<O, L> extends true ? true : CheckRepeatedChars<R, O> : false
Solution by wuxin0011 #27339
type CheckRepeatedChars<T extends string, B extends boolean = false> =
T extends `${infer F}${infer R}`
? R extends `${string}${F}${string}`
? true
: CheckRepeatedChars<R, B>
: B
Solution by smileboyi #27193
type StringToTuple<S extends string> = S extends `${infer F extends string}${infer Rest}`
? F | StringToTuple<Rest>
: never
type CheckRepeatedChars<T extends string, F extends string = ''> =
T extends `${F}${infer S extends string}${infer Rest extends string}`
? S extends StringToTuple<`${F}${Rest}`>
? true
: CheckRepeatedChars<T, `${F}${S}`>
: false
Solution by Minato1123 #27069
type OneByString<T extends string,U extends string> = T extends `${infer S}${infer R}` ? S extends U ? true : OneByString<R,U> : false
type CheckRepeatedChars<T extends string> = T extends `${infer S}${infer R}` ? OneByString<R,S> extends true ? true : CheckRepeatedChars<R> : false
Solution by WangZiChu199910252255 #26901
type CheckRepeatedChars<T extends string> = T extends `${infer F}${infer R}` ? R extends `${any}${F}` ? true : CheckRepeatedChars<R> : false
Solution by owenvip #26745
type StringToUnion<S extends string> = S extends `${infer F}${infer Str}`
? F | StringToUnion<Str>
: never
type CheckRepeatedChars<T extends string> = T extends `${infer F}${infer Str}`
? F extends StringToUnion<Str>
? true
: CheckRepeatedChars<Str>
: false
Solution by XkSuperCool #26293
// 你的答案
type CheckRepeatedChars<T extends string> =
T extends `${infer X}${infer Y extends string}`
? Y extends `${infer A}${X}${infer B}`
? true
: CheckRepeatedChars<Y>
: false
Solution by kiki-zjq #25763
type CheckRepeatedChars<T extends string> = T extends `${infer First}${infer Rest}`
? Rest extends `${string}${First}${string}`
? true
: CheckRepeatedChars<Rest>
: false
Solution by NeylonR #24929
type CheckRepeatedChars<
T extends string,
A extends string = never
> =
T extends `${infer F}${infer R}`?
F extends A?
true:CheckRepeatedChars<R, A | F>
:false
Solution by jiangshanmeta #24835
type CheckRepeatedChars<T extends string, U = never> = T extends `${infer P}${infer R}`
? P extends U
? true
: CheckRepeatedChars<R, U | P>
: false
Solution by echizen #24734