-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.ts
31 lines (29 loc) · 923 Bytes
/
palindrome.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { TestEq } from "./test-utils";
// prettier-ignore
type IsPalindrome<
Str extends string,
StrBuf extends string = Str,
StrTuple extends string[] = [],
> =
StrBuf extends ""
? StrTuple extends [string]
? true
: StrTuple extends [infer H, infer T]
? H extends T
? true
: false
: StrTuple extends [infer H, ...infer M, infer T]
? H extends T
? M extends string[]
? IsPalindrome<Str, StrBuf, M>
: false
: false
: false
: StrBuf extends `${infer H}${infer T}`
? IsPalindrome<Str, T, [...StrTuple, H]>
: false;
type Test1 = TestEq<IsPalindrome<"aba">, true>;
type Test2 = TestEq<IsPalindrome<"abba">, true>;
type Test3 = TestEq<IsPalindrome<"abca">, false>;
type Test4 = TestEq<IsPalindrome<"ab">, false>;
type Test5 = TestEq<IsPalindrome<"amanaplanacanalpanama">, true>;