FunctionRacerProblemsRace

One Cut Away

EasyTwo pointersPython, JavaScript, TypeScript, Java, C++

Return True if target can be obtained from text by cutting out one contiguous piece of text and joining what is left, otherwise False. The piece may be empty (so text itself qualifies), may sit at either end, or may be all of text (so the empty target always qualifies).

What survives the cut is a prefix of text followed by a suffix of text. Match target against text from the left as far as possible and from the right as far as possible; the two matched parts must together cover all of target.

Example 1

text "abcdef"
target "abef"
returns true

Cutting out cd from abcdef leaves abef.

Example 2

text "abcdef"
target "acdf"
returns false

acdf would need both b and e removed, which are two separate pieces.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems