Skip to content

Commit

Permalink
Create 535-encode-and-decode-tinyurl.js
Browse files Browse the repository at this point in the history
Solved encode-decode-tinyurl in JS.
  • Loading branch information
aadil42 authored Dec 3, 2022
1 parent a8550cb commit 1295e75
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions javascript/535-encode-and-decode-tinyurl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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();
encodeMap.set(longUrl, shortUrl);
decodeMap.set(shortUrl, longUrl);
} else {
return encodeMap.has(longUrl);
}

return shortUrl;
};

var decode = function(shortUrl) {
return decodeMap.get(shortUrl);
};

0 comments on commit 1295e75

Please sign in to comment.