Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Arrays & Hashing

Encode and Decode Strings

MEDIUM✓ CHIP-TIMEDLC #271 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Pack a list of strings — any characters allowed, including empty strings — into a single string, then unpack it back into the exact original list. No delimiter can be assumed absent from the data itself. Judged here via a roundTrip wrapper: decode(encode(x)) must reproduce x exactly.

A list of strings — any characters allowed, including empty strings — needs packing into a single string, and that single string needs to unpack back into the exact original list.

No character or substring can be assumed missing from the data, so a naive separator like a comma can't be trusted to always mean 'boundary' rather than 'content'.

This site verifies the drill as a round trip: your encode and decode are chained together, and the list that comes out the far end must match the list that went in, element for element, in the same order.

  • the list can hold any number of strings, including zero strings
  • individual strings may contain any characters, including the empty string
  • the reconstructed list must match the original exactly, order included
  • judged as a round trip: decode(encode(list)) must equal list
EX 01
strs = ["race", "pace", "grind"]
→ ["race", "pace", "grind"]
plain words, no special characters
EX 02
strs = []
→ []
empty list of strings
EX 03
strs = [""]
→ [""]
single empty string

The hintstake only what you need

HINT 1 THE NUDGE

Joining strings with a comma looks done in one line, until a string itself contains a comma — decoding then can't tell a real separator from data. What information would let you split unambiguously no matter what's inside the strings?

HINT 2 THE STRUCTURE

If every string announces its own length before it starts, you never need to search for a separator at all — you just count characters forward from wherever you already are.

HINT 3 ONE STEP FROM THE ANSWER

Encode each string as its length, a marker character, then the raw string itself. Decode by reading digits up to the marker, taking exactly that many characters as the word, then repeating from where you left off.

Coach's boardthe pattern, step by step

LENGTH BEFORE MARKERPATTERN · LENGTH-PREFIXED CHUNKSstrs = ["race", "pace", "grind"]
race
pace
grind
PAYLOAD
— empty —
STEP 1

strs = [race, pace, grind]. Encode by prefixing each word with its length and a # marker.

STEP 1 / 9 · ← → WORK TOO

The splitstwo paces, two languages

grind/encode-and-decode-strings.py — RACE PACE
LANG ▸
PACE ▸
class Codec:
    def encode(self, strs: List[str]) -> str:
        parts = []
        for s in strs:
            parts.append(f"{len(s)}#{s}")
        return "".join(parts)

    def decode(self, s: str) -> List[str]:
        result = []
        i = 0
        n = len(s)
        while i < n:
            j = i
            while s[j] != "#":
                j += 1
            length = int(s[i:j])
            result.append(s[j + 1 : j + 1 + length])
            i = j + 1 + length
        return result


class Solution:
    def roundTrip(self, strs: List[str]) -> List[str]:
        codec = Codec()
        return codec.decode(codec.encode(strs))
TIME O(N)SPACE O(N)PYTHON · RACE PACE · 25 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED