public class Codec {
    HashMap<String,String> map ;    //<LongUrl,ShortUrl>
    public Codec(){ = new HashMap<>();

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        //making a random number throuhg math.encoding eg: 53.15 and round it off 
        //after rounding it off change it into ascii character , it that character
        //exists in the HM add another random character to the stringbuilder 
        //untill it is not contained in the HM

        //use SB as operations in SB are in O(1) and in string is o(n)
        StringBuilder shortUrl = new StringBuilder();


        return shortUrl.toString();

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        String longUrl = map.get(shortUrl);

        return longUrl.toString();

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));

downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter