-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution535.cs
58 lines (49 loc) · 1.77 KB
/
Solution535.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution535
{
/// <summary>
/// 535. Encode and Decode TinyURL - Medium
/// <a href="https://leetcode.com/problems/encode-and-decode-tinyurl">See the problem</a>
/// </summary>
public class Codec
{
private readonly Dictionary<string, string> _shortToLong = [];
private readonly Dictionary<string, string> _longToShort = [];
private const string _alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private const string _baseUrl = "http://tinyurl.com/";
private readonly Random _random = new();
// Encodes a URL to a shortened URL
public string Encode(string longUrl)
{
if (_longToShort.ContainsKey(longUrl))
{
return _longToShort[longUrl];
}
var shortUrl = GetRandomString();
while (_shortToLong.ContainsKey(shortUrl))
{
shortUrl = GetRandomString();
}
_shortToLong[shortUrl] = longUrl;
_longToShort[longUrl] = shortUrl;
return _baseUrl + shortUrl;
}
// Decodes a shortened URL to its original URL
public string Decode(string shortUrl)
{
var shortUrlKey = shortUrl.Replace(_baseUrl, string.Empty);
return _shortToLong.ContainsKey(shortUrlKey) ? _shortToLong[shortUrlKey] : string.Empty;
}
private string GetRandomString()
{
var sb = new StringBuilder();
for (var i = 0; i < 6; i++)
{
sb.Append(_alphabet[_random.Next(0, _alphabet.Length)]);
}
return sb.ToString();
}
}
}