Key-Value Store Codec
Asked at OpenAI· Screen
Reported by the problem's creator, not verified
A string s is written as its length in characters, a colon, then s itself: "hi" becomes 2:hi and "" becomes 0:. A record is a key string immediately followed by a value string, so {"a": "xyz"} is the record 1:a3:xyz. Keys and values may contain any characters, including colons, commas, digits, newlines and non-ASCII characters. Dictionaries keep insertion order, and every operation must preserve it.
Examples
Example 1
Input: init =
[], operations =[["serialize",[{"name":"John:Doe","city":"New,York"}]],["deserialize",["4:name8:John:Doe4:city8:New,York"]],["serialize",[{}]],["deserialize",[""]],["serialize",[{"":""}]],["deserialize",["0:0:"]],["serialize",[{"k":""}]],["deserialize",["1:a1:11:b2:22"]]]Output:
["4:name8:John:Doe4:city8:New,York",{"name":"John:Doe","city":"New,York"},"",{},"0:0:",{"":""},"1:k0:",{"a":"1","b":"22"}]Explanation: Each string is written as its length, a colon, then the characters:
namehas 4 characters andJohn:Doehas 8. The colon and comma inside values are just data. An empty dictionary is the empty string, and an empty key or value is written0:.