02070-medium-drop-char

Back

// 你的答案
type DropChar<S, C, U extends string = ''> = S extends `${infer F}${infer R}` ? F extends C ?  DropChar<R, C, U> : DropChar<R, C, `${U}${F}`> : U;

Solution by HelloGGG #33425

type DropChar<S extends string, C extends string, R extends string = ''> = S extends `${C}${infer L}` 
  ? DropChar<L, C, L> 
  : S extends `${infer F}${C}${infer L}` ? DropChar<`${F}${L}`, C, `${F}${L}`> 
    : S extends `${infer F}${C}` ? DropChar<`${F}`, C, `${F}`> 
      : R;

Solution by ZhipengYang0605 #33059

type DropChar<S, C, Result extends string = ''> = S extends `${infer First}${infer Rest}`
? DropChar<Rest, C, First extends C ? Result : `${Result}${First}`>
: Result;

Solution by PeterPanov #32973

type DropChar<S extends string, C extends string,Return extends string = ""> = S extends `${infer First}${infer Rest}`
  ? First extends C
    ? DropChar<`${Rest}`,C, `${Return}`>
    : DropChar<`${Rest}`,C, `${Return}${First}`>
  : Return

Solution by yuanruixin #32887

// your answers
type DropChar<S, C extends string> = S extends `${infer F}${C}${infer L}`
  ? DropChar<`${F}${L}`, C>
  : S

Solution by pea-sys #32761

처음 풀이

type DropChar<S extends string, C, R extends string = ''> = 
  S extends `${infer First}${infer Rest}`
    ? First extends C 
      ? DropChar<Rest, C, R>
      : DropChar<Rest, C, `${R}${First}`>
    : R

다른 풀이

type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? DropChar<`${L}${R}`, C> : S;

${infer L}${C}${infer R} 구조를 이용해서 문자열 find의 기능을 할 수 있는 것을 배웠다. infer L 이 단순히 순서상 맨 앞에 있다고 첫 번째 문자열을 의미하는게 아니라는 것에 놀랐다

Solution by dev-hobin #32488

type DropChar<S, C extends string> = S extends `${infer F}${C}${infer L}`
  ? DropChar<`${F}${L}`, C>
  : S

Solution by nivenice #32397

type DropChar<S extends string, C extends string> = S extends `${infer F}${C}${infer L}` ? DropChar<`${F}${L}`, C> : S;

Solution by gasmg #31959

type DropChar<S, C extends string> = 
  S extends `${infer A}${C}${infer R}` 
  ? `${A}${DropChar<R, C>}`
  : S extends `${C}${infer R}` 
    ? DropChar<R, C> 
    : S;

Solution by ricky-fn #31840

type DropChar<S, C extends string> = 
   S extends `${infer First}${C}${infer Rest}` 
   ? `${First}${DropChar<Rest, C>}` 
   : S

Solution by gearonix #31133

type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? DropChar<`${L}${R}`, C> : S;

Solution by HeeYeonKim98 #30839

type IsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false 

type Con2Tuple<S extends string, C extends string, U extends ReadonlyArray<string> = []> = S extends `${ infer F }${ infer R }` ? IsEqual<F, C> extends true ? [...U, ...Con2Tuple<R, C, U>] : [...U, F, ...Con2Tuple<R, C, U>] : [...U]

type Tuple2Con<U extends ReadonlyArray<string>> = U extends [infer F extends string, ...infer R extends ReadonlyArray<string>] ? `${F}${Tuple2Con<R>}` : ''

type DropChar<S extends string, C extends string> = Tuple2Con<Con2Tuple<S, C>>

Solution by Yirujet #30800

type DropChar<S, C extends string> = S extends `${infer F}${C}${infer T}`
  ? DropChar<`${F}${T}`, C>
  : S

Solution by Chan-Yuxi #30758

type DropChar<S, C> = S extends `${infer F}${infer Rest}` 
  ? F extends C 
    ? `${DropChar<Rest, C>}`
    : `${F}${DropChar<Rest, C>}`
  : S;

Solution by kai-phan #30749

type DropChar<S, C> = 
  S extends `${infer First}${infer Rest}` ?
    First extends C ?
      DropChar<Rest,C> :
      `${First}${DropChar<Rest,C>}` :
    S

Solution by maximallain #30002

复用已有的ReplaceAll

type ReplaceAll<T extends string, From extends string, To extends string> = T extends `${infer R1}${From}${infer R2}` ? `${ReplaceAll<R1, From, To>}${To}${ReplaceAll<R2, From, To>}` :
    T extends `${infer R1}${From}` ? `${ReplaceAll<R1, From, To>}${To}` :
        T extends `${From}${infer R2}` ? `${To}${ReplaceAll<R2, From, To>}` : T;

type DropChar<T extends string, E extends string> = ReplaceAll<T, E, ''>;

type butterfly = DropChar<'bu tt er fly', ' '>;

Solution by sundial-dreams #29478

type DropChar<S, C, R extends string = ""> = S extends `${infer A}${infer B}`
  ? `${A extends C ? DropChar<B, C, R> : DropChar<B, C, `${R}${A}`>}`
  : R;

Solution by dmytro-shumak #29435

type StrTransUn<S extends string> = S extends `${infer L}${infer E}` ? `${L}` | StrTransUn<E> : S;

type DropString<S, R extends string> = S extends `${infer F}${infer L}` ? F extends StrTransUn<R> ? `${DropString<L, R>}` : `${F}${DropString<L, R>}`: S;

Solution by MrRENGE #29273

type AbsValueParser<A extends string> = A extends `${infer V}${Unit}` ? [V, Unit]: [A, ''];
type PercentageParser<A extends string> = A extends `${infer S extends Sign}${infer R}` ? [S, ...AbsValueParser<R>] : ['', ...AbsValueParser<A>];

Solution by meiriko #29230

type DropChar<
  S extends string,
  C extends string
> = S extends `${infer Start}${C}${infer Tail}`
  ? `${Start}${DropChar<Tail, C>}`
  : S

Solution by Kying-star #29014

type DropChar<
  S extends string,
  C extends string
> = S extends `${infer Char}${infer Str}`
  ? Char extends C
    ? DropChar<Str, C>
    : `${Char}${DropChar<Str, C>}`
  : S;

Solution by DoubleWoodLin #28675

type DropChar<S, C> = S extends `${infer F}${infer R}` ? F extends C ? `${DropChar<R, C>}` : `${F}${DropChar<R, C>}` : S

Solution by hajeonghun #28673

// your answers
type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? DropChar<`${L}${R}`, C> : S;

Solution by GreattitJY #28136

type DropChar<S extends string, C extends string, _Acc extends string = ''> = 
  S extends `${infer Letter}${infer Rest}` ? 
  Letter extends C ? DropChar<Rest, C, _Acc> : DropChar<Rest, C, `${_Acc}${Letter}`> 
  : _Acc

Solution by jjswifty #27581

type Replace<S extends string, C extends string, R extends string> = S extends `${infer Prefix}${C}${infer Suffix}`
  ? `${Prefix}${R}${Replace<Suffix, C, R>}`
  : S;

type DropChar<S extends string, C extends string> = Replace<S, C, ''>;

Solution by mrdulin #27524

// your answers
type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? DropChar<`${L}${R}`, C> : S

Solution by GreattitJY #27328


type DropChar<S, C, T extends string = ''> = S extends `${infer F}${infer O}` ?
  F extends C ? DropChar<O, C, T> : DropChar<O, C, `${T}${F}`> : T

Solution by 8471919 #27012

type DropChar<S extends string, C extends string> = S extends `${infer F}${infer R}` ? `${F extends C ? '' : F}${DropChar<R, C>}` : ''

Solution by smileboyi #26877

type DropChar<T extends string, U extends string> = T extends `${infer First}${U}${infer Rear}`
  ? DropChar<`${First}${Rear}`, U>
  : T;

Solution by rldnd #26852

type DropChar<
  T extends string,
  U extends string
> = T extends `${infer R1}${U}${infer R2}`
  ? `${DropChar<R1, U>}${DropChar<R2, U>}`
  : T;

Solution by ryuji-1to #26773