-
Notifications
You must be signed in to change notification settings - Fork 0
/
02257-medium-minusone.ts
36 lines (29 loc) · 1.41 KB
/
02257-medium-minusone.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
32
33
34
35
36
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type cases = [
Expect<Equal<MinusOne<1>, 0>>,
Expect<Equal<MinusOne<55>, 54>>,
Expect<Equal<MinusOne<3>, 2>>,
Expect<Equal<MinusOne<100>, 99>>,
Expect<Equal<MinusOne<1101>, 1100>>,
Expect<Equal<MinusOne<0>, -1>>,
Expect<Equal<MinusOne<9_007_199_254_740_992>, 9_007_199_254_740_991>>,
]
// ============= Your Code Here =============
type ParseInt<T extends string> = T extends `${infer Digit extends number}` ? Digit : never
type TestParseInt = ParseInt<'123'> // 123
type ReverseString<S extends string> = S extends `${infer First}${infer Rest}` ? `${ReverseString<Rest>}${First}` : ''
type TestReverseString = ReverseString<'123'> // 321
type RemoveLeadingZeros<S extends string> = S extends '0' ? S : S extends `${'0'}${infer R}` ? RemoveLeadingZeros<R> : S
type TestRemoveLeadingZeros = RemoveLeadingZeros<'000123'> // 123
type InternalMinusOne<
S extends string
> = S extends `${infer Digit extends number}${infer Rest}` ?
Digit extends 0 ?
`9${InternalMinusOne<Rest>}` :
`${[9, 0, 1, 2, 3, 4, 5, 6, 7, 8][Digit]}${Rest}`:
never
type TestInternalMinusOne = InternalMinusOne<'123'> // "023"
type MinusOne<T extends number> = T extends 0 ? -1 : ParseInt<RemoveLeadingZeros<ReverseString<InternalMinusOne<ReverseString<`${T}`>>>>>
type TestMinusOne = MinusOne<123> // 122
type test = MinusOne<9007199254740992>