Skip to main content

Command Palette

Search for a command to run...

Encode and Decode TinyURL

Published
3 min read
Encode and Decode TinyURL
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design a class to encode a URL and decode a tiny URL.

There is no restriction on how your encode/decode algorithm should work. 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.

Implement the Solution class:

  • Solution() Initializes the object of the system.

  • String encode(String longUrl) Returns a tiny URL for the given longUrl.

  • String decode(String shortUrl) Returns the original long URL for the given shortUrl. It is guaranteed that the given shortUrl was encoded by the same object.

LeetCode Problem - 535

import java.util.*;

public class Codec {
    // HashMap to store mapping of shortened URLs to original URLs
    Map<String, String> mp = new HashMap<>();
    String before; // Part of the long URL before the path
    String after; // Part of the long URL after the path

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        // Find the index of the first '/' after "https://" or "http://"
        int index = longUrl.indexOf("/", 8); // 8 is the length of "https://" or "http://"
        if (index == -1) {
            return longUrl; // If no '/' found, return the original URL as is
        }
        // Separate the URL into two parts: before and after the path
        before = longUrl.substring(0, index);
        after = longUrl.substring(index);

        // Generate a unique random string of length 6
        String shortenedUrl = before + generateUniqueRandomString(6);

        // Map the shortened URL to the original long URL in the HashMap
        mp.put(shortenedUrl, longUrl);

        // Return the shortened URL
        return shortenedUrl;
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        if (!mp.containsKey(shortUrl)) {
            return ""; // If the shortened URL is not in the map, return an empty string
        }
        // Retrieve and return the original long URL corresponding to the shortened URL
        return mp.get(shortUrl);
    }

    // Method to generate a unique random string of given length
    public static String generateUniqueRandomString(int length) {
        StringBuilder sb = new StringBuilder();
        Set<Character> generatedChars = new HashSet<>();

        // Loop until the StringBuilder has reached the desired length
        while (sb.length() < length) {
            // Generate a unique random character and add it to the StringBuilder if it's unique
            char uniqueChar = generateUniqueRandomCharacter();
            if (generatedChars.add(uniqueChar)) {
                sb.append(uniqueChar);
            }
        }

        // Convert StringBuilder to String and return
        return sb.toString();
    }

    // Method to generate a unique random character
    public static char generateUniqueRandomCharacter() {
        // Generate a UUID and return its first character (not necessarily unique in the strictest sense)
        UUID uuid = UUID.randomUUID();
        return uuid.toString().charAt(0);
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.