Skip to content

Commit 35082f7

Browse files
refactor 535
1 parent 605a981 commit 35082f7

File tree

1 file changed

+16
-26
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+16
-26
lines changed

src/main/java/com/fishercoder/solutions/_535.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,18 @@
44
import java.util.Map;
55
import java.util.Random;
66

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-
197
public class _535 {
208

219
public static class Solution1 {
2210
/**
2311
* Simple counter approach
2412
* 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.
3119
*/
3220
public class Codec {
3321
int i = 0;
@@ -49,8 +37,8 @@ public static class Solution2 {
4937
/**
5038
* Use Java built-in HashCode
5139
* 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.
5442
*/
5543
public class Codec {
5644
Map<Integer, String> map = new HashMap<>();
@@ -72,7 +60,9 @@ public String decode(String shortUrl) {
7260
}
7361

7462
public static class Solution3 {
75-
/**Use a random number*/
63+
/**
64+
* Use a random number
65+
*/
7666
Map<Integer, String> map = new HashMap<>();
7767
Random random = new Random();
7868
public static final String PREFIX = "http://tinyurl/";
@@ -97,11 +87,11 @@ public static class Solution4 {
9787
/**
9888
* Use a random but fixed length encoding
9989
* 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.
10595
*/
10696
Map<String, String> map = new HashMap<>();
10797
public static final String PREFIX = "http://tinyurl/";

0 commit comments

Comments
 (0)