Skip to content

Commit

Permalink
Update 0535-encode-and-decode-tinyurl.js
Browse files Browse the repository at this point in the history
Looking at the code I noticed that the toString function there was pointless considering the fact that js type coersion already converts the number to the string during concatenation. Also I also noticed the else block only returned true or false rather than the string as the question requested, so I added what I think is a fitting solution to that.
  • Loading branch information
Long1sland committed Jan 14, 2023
1 parent 73882c7 commit 2393936
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions javascript/0535-encode-and-decode-tinyurl.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
// problem link https://leetcode.com/problems/encode-and-decode-tinyurl
// time complexity O(1)


const encodeMap = new Map();
const decodeMap = new Map();
const base = 'http://tinyurl.com/';

var encode = function(longUrl) {
let shortUrl = ''
if(!encodeMap.has(longUrl)) {
shortUrl = (base + encodeMap.size + 1).toString();
shortUrl = base + encodeMap.size + 1
encodeMap.set(longUrl, shortUrl);
decodeMap.set(shortUrl, longUrl);
} else {
return encodeMap.has(longUrl);
}

return shortUrl;
}
return shortUrl || encodeMap.get(longUrl);
};

var decode = function(shortUrl) {
Expand Down

0 comments on commit 2393936

Please sign in to comment.