← 返回 airbnb 的题目列表Parse URL Query String into Dictionary
类型:online_judge
Problem: Parse URL Query Parameters into a Dictionary
Given a URL string url, parse its query string and return all query parameters as a key-value dictionary (map).
Rules
The query string begins after the first ?.
Each parameter is in the form key=value.
Parameters are separated by &.
If a key appears without a value (e.g., a or a=), treat its value as an empty string "".
If the URL contains no ?, or the part after ? is empty, return an empty dictionary.
You do not need to URL-decode (e.g., %20) unless you explicitly choose to support it.
I/O
Input: url (string)
Output: dict[str, str]
Constraints
1 <= len(url) <= 1e5
Tests (5)
Input:
https://example.com/path?foo=1&bar=2
Output:
{"foo":"1","bar":"2"}
Input:
https://example.com/path
Output:
{}
Input:
https://example.com/path?
Output:
{}
Input:
https://example.com/path?empty&x=
Output:
{"empty":"","x":""}
Input:
https://example.com/path?a=1&b=2&b=3
Output (assuming “last one wins”):
{"a":"1","b":"3"}
Example
Input
https://example.com/path?foo=1&bar=2
Output
{"foo":"1","bar":"2"}