Skip to content

Commit

Permalink
Create: 0535-encode-and-decode-tinyurl.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
loczek committed Dec 28, 2022
1 parent ee7a3bc commit d3cf047
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions typescript/0535-encode-and-decode-tinyurl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const encodeMap = {};
const decodeMap = {};
let size = 0;

/**
* Encodes a URL to a shortened URL.
*/
function encode(longUrl: string): string {
if (!encodeMap.hasOwnProperty(longUrl)) {
let shortUrl = size + 1;
size += 1;
encodeMap[longUrl] = shortUrl;
decodeMap[shortUrl] = longUrl;
}

return encodeMap[longUrl];
}

/**
* Decodes a shortened URL to its original URL.
*/
function decode(shortUrl: string): string {
return decodeMap[shortUrl];
}

/**
* Your functions will be called as such:
* decode(encode(strs));
*/

0 comments on commit d3cf047

Please sign in to comment.