-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path535.java
More file actions
26 lines (22 loc) · 722 Bytes
/
535.java
File metadata and controls
26 lines (22 loc) · 722 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
26
public class Codec {
// Encodes a URL to a shortened URL.
private HashMap<String, String> h = new HashMap<>();
private int count = 0;
public String encode(String longUrl) {
h.put(longUrl, Integer.toString(count));
this.count++;
return Integer.toString(count-1);
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
for(String k : h.keySet()){
if(h.get(k).equals(shortUrl)){
return k;
}
}
return null;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));