March LeetCoding Challenge 2021 — Day 15: Encode and Decode TinyURL
Tags: computerscience, codenewbie, algorithms, programming
Published: 2021-03-17 06:15:01



Today, we will solve the 15th problem of the March LeetCoding Challenge.


## Problem Statement

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.](http://tinyurl.com/4e9iAk.)

Design the encode and decode methods for the TinyURL service. 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.

## Solution

In this problem, we have to design an URL shortening service to encode and decode URLs. Therefore for a given longURL we should be able to get the shortURL with encode method and vice versa in the decode method.

So we can use the in-built hashcode method, because that will generate a unique value for any given string. We can store the hashcode value in a HashMap and retrieve it in the decode method.

The code is given below.
{% gist https://gist.github.com/sksaikia/94ce86475b55360017b832045cd8008b.js %}
The code can be found here
{%github sksaikia/LeetCode %}

Check out my other posts on March LeetCoding Challenge 2021.

 1. [March LeetCoding Challenge — Day 1 — Distribute Candies](https://medium.com/dev-genius/march-leetcoding-challenge-2021-problem-1-distribute-candies-f37f66ea7ee9)

 2. [March LeetCoding Challenge — Day 2 — Set Mismatch](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-2-set-mismatch-4abd5ee491c9)

 3. [March LeetCoding Challenge — Day 3 — Missing Number](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-3-missing-number-ae8ee45a58cb)

 4. [March LeetCoding Challenge — Day 4 — Intersection of Two Linked Lists](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-4-intersection-of-two-linked-lists-a775449b5563)

 5. [March LeetCoding Challenge — Day 5 — Average of Levels in Binary Tree](https://link.medium.com/sC9L595opeb)

 6. [March LeetCoding Challenge — Day 6 — Short Encoding of Words](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-6-short-encoding-of-words-7fed4bfae557)

 7. [March LeetCoding Challenge — Day 7 — Design HashMap](https://levelup.gitconnected.com/leetcode-706-design-hashmap-march-leetcoding-challenge-2021-fdae1a4adbc)

 8. [March LeetCoding Challenge — Day 8 — Remove Palindromic Subsequences](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-8-remove-palindromic-subsequences-12b037705722)

 9. [March LeetCoding Challenge — Day 10 — Integer to Roman](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-10-integer-to-roman-76caa87a0e07)

 10. [March LeetCoding Challenge — Day 12 — Check If a String Contains All Binary Codes of Size K](https://medium.com/dev-genius/march-leetcoding-challenge-2021-day-12-check-if-a-string-contains-all-binary-codes-of-size-k-48f1fed4d9b9)

 11. [March LeetCoding Challenge — Day 14-Swapping Nodes in a Linked List](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-14-swapping-nodes-in-a-linked-list-a540785c816f)