Message Serializer and Deserializer
Problem statement
Implement a reversible codec for an ordered list of text messages. Process each operation independently and return one row for each operation, in the same order.
["ENCODE", message1, message2, ...]: serialize the messages into one string. Return a row containing only that string.["DECODE", encoded]: deserialize a valid serialized string. Return the decoded messages as one row, preserving their order and all characters.
To encode one message, write its character count in ordinary decimal notation, then #, then the message itself. Concatenate these records without extra separators. The decimal count has no leading zeros, except that zero is written as 0.
Messages may be empty and may contain digits, spaces and #. For example, ["ab#", ""] encodes as 3#ab#0#. An empty message list encodes as the empty string; decoding the empty string produces an empty row.
Every DECODE input is a valid complete encoding under these rules. You do not need to detect malformed data or maintain state between operations.
Function
processMessageCodec(operations: String[][]) → String[][]Examples
Example 1
operations = [["ENCODE","hello","#",""],["DECODE","5#hello1##0#"]]return = [["5#hello1##0#"],["hello","#",""]]The length prefixes are 5, 1 and 0. The # message is payload, so it does not create another boundary.
Example 2
operations = [["ENCODE"],["DECODE",""]]return = [[""],[]]An empty message list serializes to an empty string, and that string decodes to an empty list.
Example 3
operations = [["DECODE","12#abcdefghijkl2#12"],["ENCODE","12","ab#"]]return = [["abcdefghijkl","12"],["2#123#ab#"]]A two-digit count describes the first message. Payload digits and a payload delimiter are copied without interpretation.
Constraints
1 <= operations.length <= 2000.- Every operation has one of the two forms described above.
- Each message contains printable ASCII characters and has length at most
100000. - The total number of messages encoded or decoded across all operations is at most
20000. - The total length of all message and encoded-string arguments is at most
200000characters.