Algorithm
Problem Name: 535. Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL.
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 deconding it.
Constraints:
1 <= url.length <= 104
url
is guranteed to be a valid URL.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
string encode(string longUrl) {
v.push_back(longUrl);
return to_string(v.size() - 1);
}
string decode(string shortUrl) {
return v[stoi(shortUrl)];
}
private:
vector < string>v;
};
class Solution {
public:
string encode(string longUrl) {
return longUrl;
}
string decode(string shortUrl) {
return shortUrl;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
public class Codec {
Map map = new HashMap<>();
Map < Integer, String> revMap = new HashMap<>();
int count = 0;
String BASE_URL = "http://tinyurl.com/";
public String encode(String longUrl) {
if (!map.containsKey(longUrl)) {
count++;
map.put(longUrl, count);
revMap.put(count, longUrl);
}
return BASE_URL + String.valueOf(map.get(longUrl));
}
public String decode(String shortUrl) {
Integer code = Integer.parseInt(shortUrl.substring(shortUrl.lastIndexOf('/') + 1));
return revMap.get(code);
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with C# Programming
Code -
C# Programming
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0535_EncodeAndDecodeTinyURL
{
private const string ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string DOMAIN = "http://tinyurl.com/";
private readonly IDictionary < string, string> map = new Dictionary();
private readonly Random random = new Random();
public string GetKey()
{
var sb = new StringBuilder();
for (int i = 0; i < 8; i++)
sb.Append(ALPHABET[random.Next(62)]);
return sb.ToString();
}
// Encodes a URL to a shortened URL
public string encode(string longUrl)
{
var key = string.Empty;
do
{
key = GetKey();
} while (map.ContainsKey(key));
map[key] = longUrl;
return DOMAIN + key;
}
// Decodes a shortened URL to its original URL.
public string decode(string shortUrl)
{
var key = shortUrl.Replace(DOMAIN, "");
map.TryGetValue(key, out var value);
return value;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output