문제
JSON is the text format most programs use to exchange data. A JSON object is written as {"key": "value", ...}.
Given matching lists of keys and values (all text), build the JSON object string in this exact format:
- Start with
{and end with}. - Write each pair as
"key": "value", in the given order, separated by,(comma and space). - Inside a string, write a backslash
\as\\and a double quote"as\". Other characters stay as they are.
Implement json_object_string(keys, values).
Input
keys, values: Lists of the same length.
Output
Return the JSON text. An empty object is {}.
제약 조건
0 ≤ number of pairs ≤ 100
Keys and values have at most 50 printable characters
예시
입력:
keys = ["name", "city"]
values = ["Ada", "London"]
예상 출력:
expected = "{\"name\": \"Ada\", \"city\": \"London\"}"
입력:
std::vector<std::string> keys = {"name", "city"};
std::vector<std::string> values = {"Ada", "London"};
예상 출력:
std::string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";
입력:
string[] keys = new string[] {"name", "city"};
string[] values = new string[] {"Ada", "London"};
예상 출력:
string expected = "{\"name\": \"Ada\", \"city\": \"London\"}";
설명
Each pair becomes "key": "value": {"name": "Ada", "city": "London"}.
입력:
keys = ["quote"]
values = ["say \"hi\""]
예상 출력:
expected = "{\"quote\": \"say \\\"hi\\\"\"}"
입력:
std::vector<std::string> keys = {"quote"};
std::vector<std::string> values = {"say \"hi\""};
예상 출력:
std::string expected = "{\"quote\": \"say \\\"hi\\\"\"}";
입력:
string[] keys = new string[] {"quote"};
string[] values = new string[] {"say \"hi\""};
예상 출력:
string expected = "{\"quote\": \"say \\\"hi\\\"\"}";
설명
The quotes inside the value are escaped with a backslash: {"quote": "say \"hi\""}.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
Build each "key": "value" pair separately, then join them.
힌트 2
Escape backslashes before quotes so you don't escape your own escapes.
힌트 3
An empty input still needs the braces: {}.
예상 복잡도
O(total length) time, O(total length) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.