← 返回 nvidia 的题目列表String Encoding / Decoding Variants
类型:qbank
Two string parsing variants appear: encode/decode a list of strings using length prefixes and delimiters, and decode a compact alphabet-count expression where `#` forms two-digit letters and `(k)` repeats the previous letter.
Requirements
Variant A: Encode / Decode String List
Input:
["foo", "lis", "jljl", "12345678901"]
Encode to a single string using length prefixes:
3#foo3#lis4#jljl11#12345678901
Then implement decode to recover the original list exactly.
Variant B: Alphabet Count Decoder
Given a compact string such as:
"12323#4(3)26#(2)"
Decode letters where:
1 -> a, 2 -> b, ..., 9 -> i.
10# -> j, ..., 26# -> z.
(k) means the previous decoded letter appears k times total or is repeated k times; clarify exact semantics.
Return a length-26 count array. The sample maps to abcwdddzz, so the count array has one a, one b, one c, three d, and two z.
Notes
For length-prefix encode/decode, avoid delimiter escaping by trusting the numeric length. Decode by reading digits until the first #, then consume exactly length characters. Complexity is O(total characters).
For alphabet-count decoding, scanning from right to left is clean because # tells you the previous two digits form one character. Parenthesized repeat counts can also be parsed from the right, applied to the next decoded symbol, then reset.
Pitfalls:
Strings may contain digits or #, so the delimiter must sit between length and payload (len#payload); appending the delimiter after the payload instead cannot be decoded when a payload is all digits.
Empty strings need 0# plus no payload.
Repeat semantics must be clarified: (3) may mean three total occurrences or three extra occurrences.
Preparation
Implement length-prefixed len#payload encoding and decoding from memory.
Write the reverse scanner for the alphabet-count variant and test 10#, 26#, and repeated single-digit letters.