forked from surajr/CodingInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodec.java
More file actions
25 lines (18 loc) · 689 Bytes
/
Codec.java
File metadata and controls
25 lines (18 loc) · 689 Bytes
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
public class Codec {
private Map<Integer, String> map = new HashMap<>();
private String host = "http://tinyurl.com/";
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
int hashKey = longUrl.hashCode();
map.put(hashKey, longUrl);
return host+hashKey;
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
int hashKey = Integer.parseInt(shortUrl.replace(host,""));
return map.get(hashKey);
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));