4
4
import java .util .Map ;
5
5
import java .util .Random ;
6
6
7
- /**
8
- * 535. Encode and Decode TinyURL
9
- *
10
- * TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl
11
- * and it returns a short URL such as http://tinyurl.com/4e9iAk.
12
- * Design the encode and decode methods for the TinyURL service.
13
- * There is no restriction on how your encode/decode algorithm should work.
14
- * You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
15
-
16
- Note: Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
17
- */
18
-
19
7
public class _535 {
20
8
21
9
public static class Solution1 {
22
10
/**
23
11
* Simple counter approach
24
12
* Analysis:
25
- * The range of URLs that can be decoded is limited by the range of Integer.
26
- * If excessively large number of URLs have to be encoded, after the range of Integer is exceeded,
27
- * integer overflow could lead to overwriting previous URL's encodings.
28
- * The length of the URL isn't necessary shorter than incoming URL.
29
- * One potential security issue with this problem is that it's very easy to predict the next code generated,
30
- * since this pattern is very easy to be detected.
13
+ * The range of URLs that can be decoded is limited by the range of Integer.
14
+ * If excessively large number of URLs have to be encoded, after the range of Integer is exceeded,
15
+ * integer overflow could lead to overwriting previous URL's encodings.
16
+ * The length of the URL isn't necessary shorter than incoming URL.
17
+ * One potential security issue with this problem is that it's very easy to predict the next code generated,
18
+ * since this pattern is very easy to be detected.
31
19
*/
32
20
public class Codec {
33
21
int i = 0 ;
@@ -49,8 +37,8 @@ public static class Solution2 {
49
37
/**
50
38
* Use Java built-in HashCode
51
39
* Analysis:
52
- * hashCode() does NOT generate unique codes for different strings, collision might happen.
53
- * As the number of URLs increase, the probability of getting collision increases which leads to failure.
40
+ * hashCode() does NOT generate unique codes for different strings, collision might happen.
41
+ * As the number of URLs increase, the probability of getting collision increases which leads to failure.
54
42
*/
55
43
public class Codec {
56
44
Map <Integer , String > map = new HashMap <>();
@@ -72,7 +60,9 @@ public String decode(String shortUrl) {
72
60
}
73
61
74
62
public static class Solution3 {
75
- /**Use a random number*/
63
+ /**
64
+ * Use a random number
65
+ */
76
66
Map <Integer , String > map = new HashMap <>();
77
67
Random random = new Random ();
78
68
public static final String PREFIX = "http://tinyurl/" ;
@@ -97,11 +87,11 @@ public static class Solution4 {
97
87
/**
98
88
* Use a random but fixed length encoding
99
89
* Analysis:
100
- * 1. This is the most optimal solution so far.
101
- * 2. The number of URLs that can be encoded can be as big as Math.pow((10 + 26*2), FIXED_LENGTH)
102
- * 3. The length of the shortened URL is fixed at a certain length, which could be a significant reduce for large URLs
103
- * 4. The performance of this scheme is pretty good, due to much smaller probability of encountering collision
104
- * 5. Predicting pattern/encoding isn't possible in this case since random numbers are used.
90
+ * 1. This is the most optimal solution so far.
91
+ * 2. The number of URLs that can be encoded can be as big as Math.pow((10 + 26*2), FIXED_LENGTH)
92
+ * 3. The length of the shortened URL is fixed at a certain length, which could be a significant reduce for large URLs
93
+ * 4. The performance of this scheme is pretty good, due to much smaller probability of encountering collision
94
+ * 5. Predicting pattern/encoding isn't possible in this case since random numbers are used.
105
95
*/
106
96
Map <String , String > map = new HashMap <>();
107
97
public static final String PREFIX = "http://tinyurl/" ;
0 commit comments