Question
An encoded string uses the form `k[segment]`, meaning that `segment` is repeated `k` times. Repeat counts are positive and may contain multiple digits. Encoded segments may be nested, while ordinary characters are copied directly.
Implement:
```python
def decode_string(encoded: str) -> str: ...
```
Examples:
- `3[a]2[bc]` becomes `aaabcbc`.
- `3[a2[c]]` becomes `accaccacc`.
- `2[abc]3[cd]ef` becomes `abcabccdcdcdef`.
Assume the input is syntactically valid. Avoid recursion-depth dependence by using an explicit stack. The running time should be proportional to the encoded input plus the produced output.
## Staged follow-ups
1. Validate malformed input and report the location of the first syntax error.
2. Support a postfix form such as `(segment){k}` and escaped delimiter characters.
3. Redesign the API for an expansion too large to fit in memory, producing chunks lazily.