Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2059 - Drop String (🎥 Video Explanation and Solution) #25352

Open
dimitropoulos opened this issue Mar 20, 2023 · 0 comments
Open

2059 - Drop String (🎥 Video Explanation and Solution) #25352

dimitropoulos opened this issue Mar 20, 2023 · 0 comments
Labels
2059 answer Share answers/solutions to a question en in English

Comments

@dimitropoulos
Copy link
Contributor

2059 - Drop String

We've already done a similar challenge, DropChar which will drop a specific sequence of characters. This one is a bit more complex because it will drop any individual character in the provided pattern. Loops within loops. Turtles all the way down.

🎥 Video Explanation

Release Date: 2023-04-21 19:00 UTC

Drop String

🔢 Code

// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'

type A1 = DropString<'butter fly!', ''>;
type B1 = 'butter fly!';
type C1 = Expect<Equal<A1, B1>>;

type A2 = DropString<'butter fly!', ' '>;
type B2 = 'butterfly!';
type C2 = Expect<Equal<A2, B2>>;

type A3 = DropString<'butter fly!', 'but'>;
type B3 = 'er fly!';
type C3 = Expect<Equal<A3, B3>>;

type A4 = DropString<' b u t t e r f l y ! ', 'but'>;
type B4 = '     e r f l y ! ';
type C4 = Expect<Equal<A4, B4>>;

type A5 = DropString<'    butter fly!        ', ' '>;
type B5 = 'butterfly!';
type C5 = Expect<Equal<A5, B5>>;

type A6 = DropString<' b u t t e r f l y ! ', ' '>;
type B6 = 'butterfly!';
type C6 = Expect<Equal<A6, B6>>;

type A7 = DropString<' b u t t e r f l y ! ', 'but'>;
type B7 = '     e r f l y ! ';
type C7 = Expect<Equal<A7, B7>>;

type A8 = DropString<' b u t t e r f l y ! ', 'tub'>;
type B8 = '     e r f l y ! ';
type C8 = Expect<Equal<A8, B8>>;

type A9 = DropString<' b u t t e r f l y ! ', 'b'>;
type B9 = '  u t t e r f l y ! ';
type C9 = Expect<Equal<A9, B9>>;

type A10 = DropString<' b u t t e r f l y ! ', 't'>;
type B10 = ' b u   e r f l y ! ';
type C10 = Expect<Equal<A10, B10>>;

// ============= Your Code Here =============
type DropString<T, U> =
  T extends `${infer Head}${infer Tail}`
  ? U extends `${string}${Head}${string}`
    ? DropString<Tail, U>
    : `${Head}${DropString<Tail, U>}`
  : '';

// ============== Alternatives ==============
type DropChar<T, U extends string> =
  T extends `${infer Left}${U}${infer Right}`
    ? DropChar<`${Left}${Right}`, U>
    : T;

type DropString<T, U> =
  U extends `${infer Head}${infer Tail}`
  ? DropString<DropChar<T, Head>, Tail>
  : T;

➕ More Solutions

For more video solutions to other challenges: see the umbrella list! #21338

@dimitropoulos dimitropoulos added answer Share answers/solutions to a question en in English labels Mar 20, 2023
@github-actions github-actions bot added the 2059 label Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2059 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant