File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,397 LeetCode solutions in JavaScript
1
+ # 1,398 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
427
427
529|[ Minesweeper] ( ./solutions/0529-minesweeper.js ) |Medium|
428
428
530|[ Minimum Absolute Difference in BST] ( ./solutions/0530-minimum-absolute-difference-in-bst.js ) |Easy|
429
429
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|
430
431
537|[ Complex Number Multiplication] ( ./solutions/0537-complex-number-multiplication.js ) |Medium|
431
432
538|[ Convert BST to Greater Tree] ( ./solutions/0538-convert-bst-to-greater-tree.js ) |Medium|
432
433
539|[ Minimum Time Difference] ( ./solutions/0539-minimum-time-difference.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments