Code RoomDecode length with cap
MediumPrep Room Coding #605

Decode length with cap

CodingAlgorithms & data structuresMid–Senior~20 min

A run-length-encoded string repeats blocks of the form <count><char>, where <count> is a positive decimal integer of one or more digits and <char> is a single lowercase letter (e.g. "3a2b1c" decodes to "aaabbc", length 6). Given the encoded string s and an integer cap, return the decoded length, but as soon as the running decoded length would exceed cap, stop and return -1. The empty string decodes to length 0. Counts can be large, so you must not materialize the decoded string.

Implement
run_length_decode_capacity(s: str, cap: int) → int
Examples
in["3a2b1c",100]out6
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 20 min
InputExpectedGot
["3a2b1c",100]6not run yetsample