Show problems as
Classic problem #1089EasyArrayTwo Pointers

Duplicate Zeros

Real-world version: Doubling the Blanks on a Fixed Strip
Tracked in this browser only
Read Duplicate Zeros on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A strip of digits reads strip. Every 0 on it is a blank.

Double each blank in place: each 0 becomes two 0s and everything after it shifts one place along. The strip keeps its original length, so whatever is pushed off the end is lost.

Return the strip afterwards.

Examples

Example 1

Input
strip = [0, 1, 2]
Output
[0, 0, 1]

The single blank becomes two, pushing everything along, and the last entry falls off the end.

Example 2

Input
strip = [5, 6, 7]
Output
[5, 6, 7]

There is no blank anywhere, so nothing moves.

Example 3

Input
strip = [0, 0]
Output
[0, 0]

The first blank doubles into both places, and the second blank's copies both fall off the end.

Constraints

  • 1 <= strip.length <= 10^4
  • 0 <= strip[i] <= 9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def duplicate_zeros(strip: list[int]) -> list[int]:
Java
public int[] duplicateZeros(int[] strip)