FastPrepExpand Decimal Digits into Runs of Ones

Expand Decimal Digits into Runs of Ones

ZipRecruiter logoZipRecruiter● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Scan text from left to right. Replace each decimal digit d with exactly d copies of the character 1. Keep every nondigit character unchanged.

Each digit is interpreted independently, so 0 contributes no characters.

Function

expandDigitsToOnes(text: String) → String

Examples

Example 1

text = "a5b3de"return = "a11111b111de"

Each digit expands independently while letters remain in place.

Example 2

text = "x0y2"return = "xy11"

Zero contributes an empty run and two contributes two ones.

Constraints

  • 0 <= text.length <= 100000
  • text contains ASCII letters and decimal digits.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String expandDigitsToOnes(String text) {
    // Write your code here.
}
text"a5b3de"
expected"a11111b111de"
Checking account…