-
Notifications
You must be signed in to change notification settings - Fork 1
/
EncodeAndDecodeTinyURL.java
36 lines (28 loc) · 1.01 KB
/
EncodeAndDecodeTinyURL.java
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
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
// https://leetcode.com/problems/encode-and-decode-tinyurl/
public class EncodeAndDecodeTinyURL {
private static final String SYMBOLS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private final Map<String, String> urls = new HashMap<>();
public String encode(String longUrl) {
String shortUrl = generateShortUrl();
while (urls.containsKey(shortUrl)) {
shortUrl = generateShortUrl();
}
urls.put(shortUrl, longUrl);
return shortUrl;
}
private String generateShortUrl() {
var shortUrl = new StringBuilder();
var random = new Random();
for (int i = 0; i < 6; i++) {
shortUrl.append(SYMBOLS.charAt(random.nextInt(SYMBOLS.length())));
}
return shortUrl.toString();
}
public String decode(String shortUrl) {
return urls.get(shortUrl);
}
}