From 2393936d0ebf79fc4e1d60ffaf1ef37d07689c43 Mon Sep 17 00:00:00 2001 From: Feyisayo Ibukun Date: Sat, 14 Jan 2023 13:04:49 +0100 Subject: [PATCH] Update 0535-encode-and-decode-tinyurl.js 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. --- javascript/0535-encode-and-decode-tinyurl.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/javascript/0535-encode-and-decode-tinyurl.js b/javascript/0535-encode-and-decode-tinyurl.js index 9377cdbad..e08802891 100644 --- a/javascript/0535-encode-and-decode-tinyurl.js +++ b/javascript/0535-encode-and-decode-tinyurl.js @@ -1,7 +1,6 @@ // 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/'; @@ -9,14 +8,11 @@ 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) {