535. Encode and Decode TinyURL Medium
1/**
2 * [535] Encode and Decode TinyURL
3 *
4 * <blockquote>Note: This is a companion problem to the <a href="https://leetcode.com/discuss/interview-question/system-design/" target="_blank">System Design</a> problem: <a href="https://leetcode.com/discuss/interview-question/124658/Design-a-URL-Shortener-(-TinyURL-)-System/" target="_blank">Design TinyURL</a>.</blockquote>
5 * TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design a class to encode a URL and decode a tiny URL.
6 * There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
7 * Implement the Solution class:
8 *
9 * Solution() Initializes the object of the system.
10 * String encode(String longUrl) Returns a tiny URL for the given longUrl.
11 * String decode(String shortUrl) Returns the original long URL for the given shortUrl. It is guaranteed that the given shortUrl was encoded by the same object.
12 *
13 *
14 * Example 1:
15 *
16 * Input: url = "https://leetcode.com/problems/design-tinyurl"
17 * Output: "https://leetcode.com/problems/design-tinyurl"
18 * Explanation:
19 * Solution obj = new Solution();
20 * string tiny = obj.encode(url); // returns the encoded tiny url.
21 * string ans = obj.decode(tiny); // returns the original url after deconding it.
22 *
23 *
24 * Constraints:
25 *
26 * 1 <= url.length <= 10^4
27 * url is guranteed to be a valid URL.
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/encode-and-decode-tinyurl/
33// discuss: https://leetcode.com/problems/encode-and-decode-tinyurl/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37struct Codec {
38
39}
40
41/**
42 * `&self` means the method takes an immutable reference.
43 * If you need a mutable reference, change it to `&mut self` instead.
44 */
45impl Codec {
46 fn new() -> Self {
47 String::new()
48 }
49
50 // Encodes a URL to a shortened URL.
51 fn encode(&self, longURL: String) -> String {
52
53 }
54
55 // Decodes a shortened URL to its original URL.
56 fn decode(&self, shortURL: String) -> String {
57
58 }
59}
60
61/**
62 * Your Codec object will be instantiated and called as such:
63 * let obj = Codec::new();
64 * let s: String = obj.encode(strs);
65 * let ans: VecVec<String> = obj.decode(s);
66 */
67
68// submission codes end
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_535() {
76 }
77}
78
Back
© 2025 bowen.ge All Rights Reserved.