All problems
0112EasyTwo PointersStringGreedy

Mosaic Border Inspection

Tracked in this browser only
Write code

Trains the technique from

LeetCode 680Valid Palindrome II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A mosaic studio finishes decorative border strips before they ship. A strip arrives as the string strip, one lowercase letter per tile naming that tile's glaze, listed from the left end of the strip to the right end.

The studio accepts a strip when it shows the same glaze sequence read from either end, and the finisher is allowed to pull out at most one tile first. Pulling a tile out closes the gap, so the two tiles that flanked it become neighbours. Pulling nothing out is also allowed, so a strip that already matches end to end is accepted as it stands.

Return true when the strip can be accepted and false when no single removal is enough.

Examples

Example 1

Input
strip = "levels"
Output
true

Pull the trailing `s` and the remaining six-tile run `level` shows the same glazes from either end.

Example 2

Input
strip = "cobalt"
Output
false

Dropping the `c` still leaves `obalt` mismatched at its ends, and dropping the `t` leaves `cobal` mismatched too, so one removal cannot rescue it.

Example 3

Input
strip = "kayak"
Output
true

The strip already matches end to end, and removing nothing is permitted.

Constraints

  • 1 <= strip.length <= 10^5
  • strip consists of lowercase English letters.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def inspect_border(strip: str) -> bool:
Java
public boolean inspectBorder(String strip)
September 7
Apply