Skip to content

Commit d1076f0

Browse files
committed
Added 0535-encode-and-decode-tinyurl.cs
1 parent b2d50df commit d1076f0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Codec
2+
{
3+
public Dictionary<string, string> EncodeMap = new Dictionary<string, string>();
4+
public Dictionary<string, string> DecodeMap = new Dictionary<string, string>();
5+
public string Base = "http://tinyurl.com/";
6+
7+
// Encodes a URL to a shortened URL
8+
public string encode(string longUrl)
9+
{
10+
if (!EncodeMap.ContainsKey(longUrl))
11+
{
12+
var shortUrl = Base + EncodeMap.Count().ToString();
13+
EncodeMap.Add(longUrl, shortUrl);
14+
DecodeMap.Add(shortUrl, longUrl);
15+
}
16+
17+
return EncodeMap[longUrl];
18+
}
19+
20+
// Decodes a shortened URL to its original URL.
21+
public string decode(string shortUrl)
22+
{
23+
return DecodeMap[shortUrl];
24+
}
25+
}
26+
27+
// Your Codec object will be instantiated and called as such:
28+
// Codec codec = new Codec();
29+
// codec.decode(codec.encode(url));

0 commit comments

Comments
 (0)