Skip to content

Commit 4eddd55

Browse files
authored
Merge pull request neetcode-gh#2029 from Long1sland/patch-1
Update 0535-encode-and-decode-tinyurl.js
2 parents 4db9b7e + 2393936 commit 4eddd55

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

javascript/0535-encode-and-decode-tinyurl.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
// problem link https://leetcode.com/problems/encode-and-decode-tinyurl
22
// time complexity O(1)
33

4-
54
const encodeMap = new Map();
65
const decodeMap = new Map();
76
const base = 'http://tinyurl.com/';
87

98
var encode = function(longUrl) {
109
let shortUrl = ''
1110
if(!encodeMap.has(longUrl)) {
12-
shortUrl = (base + encodeMap.size + 1).toString();
11+
shortUrl = base + encodeMap.size + 1
1312
encodeMap.set(longUrl, shortUrl);
1413
decodeMap.set(shortUrl, longUrl);
15-
} else {
16-
return encodeMap.has(longUrl);
17-
}
18-
19-
return shortUrl;
14+
}
15+
return shortUrl || encodeMap.get(longUrl);
2016
};
2117

2218
var decode = function(shortUrl) {

0 commit comments

Comments
 (0)