Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0532f33

Browse files
committedApr 20, 2025
Add solution #535
1 parent da9d268 commit 0532f33

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,397 LeetCode solutions in JavaScript
1+
# 1,398 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -427,6 +427,7 @@
427427
529|[Minesweeper](./solutions/0529-minesweeper.js)|Medium|
428428
530|[Minimum Absolute Difference in BST](./solutions/0530-minimum-absolute-difference-in-bst.js)|Easy|
429429
532|[K-diff Pairs in an Array](./solutions/0532-k-diff-pairs-in-an-array.js)|Medium|
430+
535|[Encode and Decode TinyURL](./solutions/0535-encode-and-decode-tinyurl.js)|Medium|
430431
537|[Complex Number Multiplication](./solutions/0537-complex-number-multiplication.js)|Medium|
431432
538|[Convert BST to Greater Tree](./solutions/0538-convert-bst-to-greater-tree.js)|Medium|
432433
539|[Minimum Time Difference](./solutions/0539-minimum-time-difference.js)|Medium|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 535. Encode and Decode TinyURL
3+
* https://leetcode.com/problems/encode-and-decode-tinyurl/
4+
* Difficulty: Medium
5+
*
6+
* TinyURL is a URL shortening service where you enter a URL such as
7+
* https://leetcode.com/problems/design-tinyurl and it returns a short URL such as
8+
* http://tinyurl.com/4e9iAk. Design a class to encode a URL and decode a tiny URL.
9+
*
10+
* There is no restriction on how your encode/decode algorithm should work. You just
11+
* need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be
12+
* decoded to the original URL.
13+
*
14+
* Implement the Solution class:
15+
* - Solution() Initializes the object of the system.
16+
* - String encode(String longUrl) Returns a tiny URL for the given longUrl.
17+
* - String decode(String shortUrl) Returns the original long URL for the given shortUrl.
18+
* It is guaranteed that the given shortUrl was encoded by the same object.
19+
*/
20+
21+
const map = new Map();
22+
let counter = 1;
23+
24+
/**
25+
* Encodes a URL to a shortened URL.
26+
*
27+
* @param {string} longUrl
28+
* @return {string}
29+
*/
30+
var encode = function(longUrl) {
31+
const shortUrl = 'https://tinyurl.com/' + counter.toString();
32+
map.set(shortUrl, longUrl);
33+
counter++;
34+
return shortUrl;
35+
};
36+
37+
/**
38+
* Decodes a shortened URL to its original URL.
39+
*
40+
* @param {string} shortUrl
41+
* @return {string}
42+
*/
43+
var decode = function(shortUrl) {
44+
return map.get(shortUrl) || null;
45+
};

0 commit comments

Comments
 (0)
Please sign in to comment.