-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLShortener.java
More file actions
116 lines (97 loc) · 3.03 KB
/
URLShortener.java
File metadata and controls
116 lines (97 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* URL Shortener implementation
* Converts long URLs into short URLs and retrieves original URLs.
* Uses HashMap for O(1) average time complexity.
*/
import java.util.HashMap;
import java.util.Random;
import java.net.MalformedURLException;
import java.net.URL;
public class URLShortener {
// Stores mapping from short code to original long URL
private HashMap<String,String> shorttolong;
// Stores mapping from long URL to short code (to handle duplicates)
private HashMap<String,String> longtoshort;
// Base URL used for generating short URLs
private final String base="http://short.ly/";
public URLShortener(){
shorttolong=new HashMap<>();
longtoshort=new HashMap<>();
}
/**
* Shortens a given long URL.
* @param longurl the original URL
* @return shortened URL or error message if invalid
*/
public String shorttolong(String longurl)
{
// Check for null or empty input
longurl=longurl.trim();
if(longurl==null || longurl.isEmpty())
return "Invalid URL";
// Validate URL format
if(!isValidUrl(longurl))
return "Invalid URL";
// Return existing short URL if already shortened
if(longtoshort.containsKey(longurl))
{
return base+longtoshort.get(longurl);
}
// Generate unique short code
String code=generatecode();
if(shorttolong.containsKey(code))
{
code=generatecode();
}
// Store mappings
shorttolong.put(code,longurl);
longtoshort.put(longurl, code);
return code;
}
/**
* Generates a random 6-character alphanumeric code.
* @return short code
*/
public String generatecode()
{
String s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqurstuvwxyz0123456789";
StringBuilder code=new StringBuilder();
Random random=new Random();
// Generate random 6-character string
for(int i=0;i<6;i++)
{
int index=random.nextInt(s.length());
code.append(s.charAt(index));
}
return code.toString();
}
/**
* Retrieves the original URL from a short URL.
* @param shorturl the shortened URL
* @return original URL or error message
*/
public String getOriginal(String shorturl)
{
shorturl=shorturl.trim();
// Validate short URL format
if(shorturl==null || shorturl.trim().isEmpty() || !shorturl.startsWith(base))
return "Invalid URL";
// Extract short code
String code = shorturl.replace(base, "");
// Fetch original URL
return shorttolong.getOrDefault(code, "URL not found");
}
/**
* Checks whether a given string is a valid URL.
* @param url input URL string
* @return true if valid, false otherwise
*/
private boolean isValidUrl(String url) {
try {
new URL(url);
return true;
} catch (MalformedURLException e) {
return false;
}
}
}