cswork / OA打开练习工作台

AirbnbOA #5

Parse Query String

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)字符串哈希计数

Given a GET request query string, parse the key-value pairs into a normalized representation. The input may start with ?. Parameters are separated by &. A parameter can have one of these forms:

  • key=value: stores the parsed value for key.
  • !key: stores the boolean value true for key. Keys may repeat. If a key appears more than once, preserve all of its values in insertion order. Values can be integers, booleans, quoted strings, or list literals. For this function, return a stable string serialization of the parsed map:
  • Return one entry per distinct key.
  • Keep keys in the order they first appear.
  • Represent a single value as key=value.
  • Represent multiple values as key=value1|value2|....
  • Remove surrounding double quotes from quoted string values.

Constraints

  • 0 ≤ query.length ≤ 10⁵
  • Keys are non-empty and contain letters, digits, underscores, or hyphens.
  • Values do not contain unescaped &.

Example 1

Input:

query = "?key1=1&key1=\"abc\"&key2=value1&!isBooleanField"

Output:

["key1=1|abc","key2=value1","isBooleanField=true"]

Explanation: key1 appears twice, so both values are preserved. The boolean shorthand !isBooleanField stores true.

Example 2

Input:

query = "page=2&active=false&tags=[red,blue]&page=3"

Output:

["page=2|3","active=false","tags=red|blue"]

Explanation: The parser keeps the first-seen key order: page, active, then tags.