Skip to content

Commit e35d617

Browse files
authored
Merge pull request neetcode-gh#1518 from aadil42/patch-27
Create 535-encode-and-decode-tinyurl.js
2 parents fbaa3b4 + 1295e75 commit e35d617

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// problem link https://leetcode.com/problems/encode-and-decode-tinyurl
2+
// time complexity O(1)
3+
4+
5+
const encodeMap = new Map();
6+
const decodeMap = new Map();
7+
const base = 'http://tinyurl.com/';
8+
9+
var encode = function(longUrl) {
10+
let shortUrl = ''
11+
if(!encodeMap.has(longUrl)) {
12+
shortUrl = (base + encodeMap.size + 1).toString();
13+
encodeMap.set(longUrl, shortUrl);
14+
decodeMap.set(shortUrl, longUrl);
15+
} else {
16+
return encodeMap.has(longUrl);
17+
}
18+
19+
return shortUrl;
20+
};
21+
22+
var decode = function(shortUrl) {
23+
return decodeMap.get(shortUrl);
24+
};

0 commit comments

Comments
 (0)