Last updated on February 4th, 2025 at 12:50 am
Here, we see an Encode and Decode TinyURL LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Hash Table, Math
Companies
Amazon, Facebook, Google, Uber
Level of Question
Medium

Encode and Decode TinyURL LeetCode Solution
Table of Contents
1. Problem Statement
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.
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.
Implement the Solution
class:
Solution()
Initializes the object of the system.String encode(String longUrl)
Returns a tiny URL for the givenlongUrl
.String decode(String shortUrl)
Returns the original long URL for the givenshortUrl
. It is guaranteed that the givenshortUrl
was encoded by the same object.
Example 1:
Input: url = “https://leetcode.com/problems/design-tinyurl”
Output: “https://leetcode.com/problems/design-tinyurl”
Explanation:
Solution obj = new Solution(); string tiny = obj.encode(url); // returns the encoded tiny URL.
string ans = obj.decode(tiny); // returns the original url after decoding it.
2. Coding Pattern Used in Solution
The provided code implements a “Hashing/Mapping” pattern. The core idea is to use a hash map (or dictionary) to store mappings between long URLs and their shortened versions (or vice versa). This allows for efficient encoding and decoding operations.
3. Code Implementation in Different Languages
3.1 Encode and Decode TinyURL C++
class Solution { public: map<int, string> m; string encode(string longUrl) { m[m.size()] = longUrl; return "http://tinyurl.com/" + to_string(m.size() - 1); } string decode(string shortUrl) { int idx = shortUrl.rfind('/'); return m[stoi(shortUrl.substr(idx + 1))]; } };
3.2 Encode and Decode TinyURL Java
public class Codec { Map<String, String> codeDB = new HashMap<>(), urlDB = new HashMap<>(); static final String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; private String getCode() { char[] code = new char[6]; for (int i = 0; i < 6; i++) code[i] = chars.charAt((int)(Math.random() * 62)); return "http://tinyurl.com/" + String.valueOf(code); } public String encode(String longUrl) { if (urlDB.containsKey(longUrl)) return urlDB.get(longUrl); String code = getCode(); while (codeDB.containsKey(code)) code = getCode(); codeDB.put(code, longUrl); urlDB.put(longUrl, code); return code; } public String decode(String shortUrl) { return codeDB.get(shortUrl); } }
3.3 Encode and Decode TinyURL JavaScript
let hashMap = new Map() var encode = function(longUrl) { const code = Math.random().toString(32).slice(5) const tinyUrl = `https://tinyurl.com/${code}` hashMap.set(tinyUrl,longUrl) return tinyUrl }; var decode = function(shortUrl) { return hashMap.get(shortUrl) };
3.4 Encode and Decode TinyURL Python
class Codec: codeDB, urlDB = defaultdict(), defaultdict() chars = string.ascii_letters + string.digits def getCode(self): code = ''.join(random.choice(self.chars) for i in range(6)) return "http://tinyurl.com/" + code def encode(self, longUrl): if longUrl in self.urlDB: return self.urlDB[longUrl] code = self.getCode() while code in self.codeDB: code = getCode() self.codeDB[code] = longUrl self.urlDB[longUrl] = code return code def decode(self, shortUrl): return self.codeDB[shortUrl]
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(1) | O(n) |
Java | O(1) | O(n) |
JavaScript | O(1) | O(n) |
Python | O(1) | O(n) |