#C173. URL Shortener
URL Shortener
URL Shortener
You are given the task to implement a URL Shortener service. The service converts a long URL into a short, unique code and also retrieves the original URL when provided with the short code. The same long URL should always get the same short code. Internally, you may use a counter and convert it to a base-62 representation, where the conversion formula is given by $$\text{short\_code} = \text{base62}(\text{counter})$$ with $$\text{base} = 62$$ and the character set defined as all lowercase letters, uppercase letters, and digits.
The program will process a series of operations. Each operation is one of the following:
- Encode Operation: Given a long URL, convert it to a short URL. (Input prefix "1")
- Decode Operation: Given a short URL, return the original long URL. If it does not exist, output an empty string. (Input prefix "2")
- If the line starts with
1
followed by a space and a URL, you need to encode the long URL. - If the line starts with
2
followed by a space and a short URL, you need to decode it.
All input is received from standard input and output must be sent to standard output.
inputFormat
The first line contains an integer N representing the number of operations. Each of the following N lines contains an operation:
Note: The URLs can be any string including the empty string.
outputFormat
For each operation, output the result on a new line. For an encode operation, output the generated short URL. For a decode operation, output the corresponding long URL (or an empty string if not found).
## sample7
1 https://www.example.com/articles/how-to-code-in-python
1 https://www.example.com/articles/learn-java
1 https://www.example.com/articles/learn-cpp
1 https://www.example.com/articles/how-to-code-in-python
2 b
2 c
2 e
b
c
d
b
https://www.example.com/articles/how-to-code-in-python
https://www.example.com/articles/learn-java
</p>