How to design a system that takes big URLs like “https://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/” and converts them into a short 6 character URL. It is given that URLs are stored in the database and every URL has an associated integer id.
One important thing to note is, the long URL should also be uniquely identifiable from the short URL. So we need a Bijective Function
One Simple Solution could be Hashing. Use a hash function to convert long string to short string. In hashing, that may be collisions (2 long URLs map to same short URL) and we need a unique short URL for every long URL so that we can access long URL back.
A Better Solution is to use the integer id stored in the database and convert the integer to a character string that is at most 6 characters long. This problem can basically seen as a base conversion problem where we have a 10 digit input number and we want to convert it into a 6-character long string.
Below is one important observation about possible characters in URL.
A URL character can be one of the following
- A lower case alphabet [‘a’ to ‘z’], total 26 characters
- An upper case alphabet [‘A’ to ‘Z’], total 26 characters
- A digit [‘0’ to ‘9’], total 10 characters
There are total 26 + 26 + 10 = 62 possible characters.
So the task is to convert a decimal number to base 62 number.
To get the original long URL, we need to get URL id in the database. The id can be obtained using base 62 to decimal conversion.
Implementation:
C++
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string idToShortURL( long int n)
{
char map[] = "abcdefghijklmnopqrstuvwxyzABCDEF"
"GHIJKLMNOPQRSTUVWXYZ0123456789" ;
string shorturl;
while (n)
{
shorturl.push_back(map[n%62]);
n = n/62;
}
reverse(shorturl.begin(), shorturl.end());
return shorturl;
}
long int shortURLtoID(string shortURL)
{
long int id = 0;
for ( int i=0; i < shortURL.length(); i++)
{
if ( 'a' <= shortURL[i] && shortURL[i] <= 'z' )
id = id*62 + shortURL[i] - 'a' ;
if ( 'A' <= shortURL[i] && shortURL[i] <= 'Z' )
id = id*62 + shortURL[i] - 'A' + 26;
if ( '0' <= shortURL[i] && shortURL[i] <= '9' )
id = id*62 + shortURL[i] - '0' + 52;
}
return id;
}
int main()
{
int n = 12345;
string shorturl = idToShortURL(n);
cout << "Generated short url is " << shorturl << endl;
cout << "Id from url is " << shortURLtoID(shorturl);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static String idToShortURL( int n)
{
char map[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" .toCharArray();
StringBuffer shorturl = new StringBuffer();
while (n > 0 )
{
shorturl.append(map[n % 62 ]);
n = n / 62 ;
}
return shorturl.reverse().toString();
}
static int shortURLtoID(String shortURL)
{
int id = 0 ;
for ( int i = 0 ; i < shortURL.length(); i++)
{
if ( 'a' <= shortURL.charAt(i) &&
shortURL.charAt(i) <= 'z' )
id = id * 62 + shortURL.charAt(i) - 'a' ;
if ( 'A' <= shortURL.charAt(i) &&
shortURL.charAt(i) <= 'Z' )
id = id * 62 + shortURL.charAt(i) - 'A' + 26 ;
if ( '0' <= shortURL.charAt(i) &&
shortURL.charAt(i) <= '9' )
id = id * 62 + shortURL.charAt(i) - '0' + 52 ;
}
return id;
}
public static void main (String[] args) throws IOException
{
int n = 12345 ;
String shorturl = idToShortURL(n);
System.out.println( "Generated short url is " + shorturl);
System.out.println( "Id from url is " +
shortURLtoID(shorturl));
}
}
|
Python3
def idToShortURL( id ):
map = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
shortURL = ""
while ( id > 0 ):
shortURL + = map [ id % 62 ]
id / / = 62
return shortURL[ len (shortURL): : - 1 ]
def shortURLToId(shortURL):
id = 0
for i in shortURL:
val_i = ord (i)
if (val_i > = ord ( 'a' ) and val_i < = ord ( 'z' )):
id = id * 62 + val_i - ord ( 'a' )
elif (val_i > = ord ( 'A' ) and val_i < = ord ( 'Z' )):
id = id * 62 + val_i - ord ( 'A' ) + 26
else :
id = id * 62 + val_i - ord ( '0' ) + 52
return id
if (__name__ = = "__main__" ):
id = 12345
shortURL = idToShortURL( id )
print ( "Short URL from 12345 is : " , shortURL)
print ( "ID from" , shortURL, "is : " , shortURLToId(shortURL))
|
C#
using System;
public class GFG
{
static String idToShortURL( int n)
{
char []map = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" .ToCharArray();
String shorturl = "" ;
while (n > 0)
{
shorturl+=(map[n % 62]);
n = n / 62;
}
return reverse(shorturl);
}
static String reverse(String input) {
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
static int shortURLtoID(String shortURL)
{
int id = 0;
for ( int i = 0; i < shortURL.Length; i++)
{
if ( 'a' <= shortURL[i] &&
shortURL[i] <= 'z' )
id = id * 62 + shortURL[i] - 'a' ;
if ( 'A' <= shortURL[i] &&
shortURL[i] <= 'Z' )
id = id * 62 + shortURL[i] - 'A' + 26;
if ( '0' <= shortURL[i] &&
shortURL[i] <= '9' )
id = id * 62 + shortURL[i] - '0' + 52;
}
return id;
}
public static void Main(String[] args)
{
int n = 12345;
String shorturl = idToShortURL(n);
Console.WriteLine( "Generated short url is " + shorturl);
Console.WriteLine( "Id from url is " +
shortURLtoID(shorturl));
}
}
|
Javascript
<script>
function idToShortURL(n)
{
let map = "abcdefghijklmnopqrstuvwxyzABCDEF"
"GHIJKLMNOPQRSTUVWXYZ0123456789" ;
let shorturl = [];
while (n)
{
shorturl.push(map[n % 62]);
n = Math.floor(n / 62);
}
shorturl.reverse();
return shorturl.join( "" );
}
function shortURLtoID(shortURL) {
let id = 0;
for (let i = 0; i < shortURL.length; i++) {
if ( 'a' <= shortURL[i] && shortURL[i] <= 'z' )
id = id * 62 + shortURL[i].charCodeAt(0) - 'a' .charCodeAt(0);
if ( 'A' <= shortURL[i] && shortURL[i] <= 'Z' )
id = id * 62 + shortURL[i].charCodeAt(0) - 'A' .charCodeAt(0) + 26;
if ( '0' <= shortURL[i] && shortURL[i] <= '9' )
id = id * 62 + shortURL[i].charCodeAt(0) - '0' .charCodeAt(0) + 52;
}
return id;
}
let n = 12345;
let shorturl = idToShortURL(n);
document.write( "Generated short url is " + shorturl + "<br>" );
document.write( "Id from url is " + shortURLtoID(shorturl));
</script>
|
OutputGenerated short url is dnh
Id from url is 12345
Time complexity : O(n)
Auxiliary Space : O(1)
Optimization: We can avoid reverse step in idToShortURL(). To make sure that we get the same ID back, we also need to change shortURLtoID() to process characters from the end instead of the beginning.