Leetcode Solution

Featured Image

Valid Parentheses | TypeScript Solution

In this post, I am going to talk about the Valid Parentheses problem, which you can find in Leetcode. For this problem I have used TypeScript to solve but you can use any language to solve this problem. It is fairly easy to solve. To solve this problem we need to use Stack. You can learn more about the stack data structure from here.

Full Solution for the Valid Parentheses problem

Valid Parenthesis problem solution

Check out my other solutions two sumValid Anagram and many more.

Valid Parentheses | TypeScript Solution Read More »

Feature image

Best Time to Buy and Sell Stock | Typescript Solution

In this post I am going to share how I solved the leetcode problem called Best Time To Buy and Sell Stock. Although this problem is marked as easy but if you do not understand how the sliding window works than it may seems a little bit tough.

What is Sliding Window

Let’s say you have an array or a list and your task is to find a suitable sub section of that array that satisfies a specific condition. For example, we can think that we have an array [1,2,3,4,5,6,7]. We need to find a subset (a portion of an array) where the addition of all the item is 7. In this scenario, let’s say the subset should contain only 2 items. Whenever we say a subset, most of the times it means those values should be together, side by side. That’s how I determine if I can solve a problem using Sliding window. You can take any two of them that are side by side and check if there sum is 7 or not. Here is a representation below for better understanding.

In here we follow this steps:

  1. Take index 0 and 1, Is there sum equals 7?
  2. If no, take index 1 and 2 and check again
  3. if no, take index 2 and 3 and check again,

We continue to do it until we reach the end of the array or whenever we the condition is satisfied

Ok got it. Now how can we solve the best time to buy and sell stock problem?

Now, In our case, we are supposed to do the same thing to solve this problem. We need to find out a sub array where the max amount of profit can be made. Or in other words, find a sub set of array elements that produces the highest number of value but adding them together.

So we at first set the left index as 0 and the right index as 1. We also assume that the max is 0. The left index is yesterday and the right index is today. Notice that we can not go to the previous day in stock price calculation. So whenever we see that the price of today is lower than whatever value we have in the left index, it means we are making a loss. If we are making a loss that we do not need to check any further. We point the left index value to the right index and start continuing from there again. And we continue to increase the window size. But if we see that we are making profit, we check if it the the max so far or not and store it based on that result.

Once we go through all the value we will have the max profit we can have. I have added a nice little animation to show how it works in action.

Animation of using sliding window to solve the problem

Full solution

Solution for Best time to buy and sell stock
Solution for the problem Best time to buy and sell stock

Check out my other solutions two sum, Valid Anagram and many more.

Best Time to Buy and Sell Stock | Typescript Solution Read More »

Feature image

Valid Palindrome – Leetcode | TypeScript Solution

In my previous post where I discussed about the two sum problem, I told that the solution for the Valid Palindrome and anagram are somewhat similar. However, the palindrome is much more easier than anagram. In this problem, we have to know if a phrase is a palindrome or not. For those who do not know what is Palindrome, “A phrase is a palindrome if  it reads the same forward and backward, after removing all the spaces and non-alphanumeric characters and it shouldn’t be case sensitive. “

From the definition, we can understand couple of things

  1. We have to transform the given text to either lowercase or uppercase
  2. We have to remove all the spaces
  3. We only need to get the words no extra non alpha characters


So we will first transformed all the characters to lowercase by using a lodash function called lowerCase. Once that is done, we will remove all the spaces by writing the below code

 const removedSpaces = lowerCase.split(' ').join('');

Once that is done now we have to remove all the extra non-alphanumeric characters. I was looking at the lodash library with does have a very good function called words. I really liked the functionality so I used that one. Now that I have removed all the clutter, all I have to do is to reverse the original string and check if the original and the reversed string are same. I have wrote the whole code in TypeScript and added it below.

Solution

Solution for the Valid Palindrome
Solution for the valid palindrome

Hello readers, If you are like me who wants to get better at coding, please check out my other blog posts where I solve other leetcode problems.

In the next post, I am going to solve 3Sum problem. You can also check out my other blog post as well.

Valid Palindrome – Leetcode | TypeScript Solution Read More »

Feature image

Two Sum – Leetcode Solution using JavaScript

Hello everyone. In this post, I am going to solve Two Sum problem from leetcode. This problem is related to arrays and hashing.

In this problem, given a target value, we have to find out the indices of the two numbers such that they add up to target. So for example if the array is [2,7,11,15] and the target is 9, then we have to return the index value [0,1]. Because the index value of 0 and 1 is 2 and 7. the addition of 2 and 7 is 9.

I solved the problem using JavaScript and I went with the straight forward way to solve it. It passed the submission! That’s why I did not think about any optimization or any other solution.

To solve this problem, I created a loop and inside that loop I created an inner loop. Basically checking every combination until I get a match. I do not think I need to explain any thing for this code. Please take a look at it and if you have any problem understanding please comment on the post. I will try my best to explain.

Solution

Two Sum Solution using JavaScript
Two Sum Solution

Hello readers, If you are like me who wants to get better at coding, please check out my other blog posts where I solve other leetcode problems.

In the next post, I am going to solve Valid palindrome. Although It is similar to Valid Anagram, if you face trouble check out my solution. There are a few slight changes in the condition.

If you like my solution, please check out my other post about various leetcode solutions. If you want me to solve any other leetcode problem, feel free to comment it below.

Two Sum – Leetcode Solution using JavaScript Read More »

Valid Anagram Feature Image

Valid Anagram Solution | Typescript | Leetcode problem

In this post, I am going to solve the Leetcode problem called Valid Anagram and provide solution using TypeScript. In this problem, given that we have 2 strings, we have to tell whether they are Anagram or not. For those who don’t know, An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Though process for the valid anagram problem

Before trying to solve we can find a few clues about anagrams.

  1. As I stated earlier, both of the string will have same amount of letter.
  2. The number of letter occurrences will be same.

To solve a problem, I always check for the simple conditional part. That way, I can take out the obvious test cases which would fail. In this case, I need to check both of the strings are of same length. If not, then they are not anagram. Plain and simple. Which is why I wrote this line in the beginning of the code.

if(s.length !== t.length) return false;

For the second point, we need to check how many times a character/letter used. So I wrote a simple for loop that basically checks every character one by one and they it checks in it’s own dictionary to see if that was used before or not. If yes, I increase the value by one. If not, I add a new property in that dictionary. (Note: In JS/TS there is no direct way to use dictionary like python/C#, so we have to create an object and use it like a dictionary). Here is the code for that.

for(let i = 0; i<text.length; i++){
        const ch = text.charAt(i);

        if (ch in dict) {
            dict[ch] += 1;
        } else {
            dict[ch] = 1
        }
    }

I encapsulated the dictionary creation part into a nice little function so that it is easier to read the code. Once I am able to create the dictionary for both the original and the test string, I called a lodash function called isEqual. It performs a deep comparison between two objects. Here is the full code for the valid anagram solution.

Full Valid Anagram Solution

function isAnagram(s: string, t: string): boolean {
    if(s.length !== t.length) return false;
    
    const input = getDictionary(s);
    const testInput = getDictionary(t);
    
    return _.isEqual(input, testInput);
};

function getDictionary(text: string) {
    let dict: { [letter: string]: number } = {};
    
    for(let i = 0; i<text.length; i++){
        const ch = text.charAt(i);

        if (ch in dict) {
            dict[ch] += 1;
        } else {
            dict[ch] = 1
        }
    }
    
    return dict;
}

Hello readers, If you are like me who wants to get better at coding, please check out my other blog posts where I solve other leetcode problems.

In the next post, I am going to solve Two Sum problem. In the meantime, you can checkout the post I have written on another problem called Contains Duplicate

Valid Anagram Solution | Typescript | Leetcode problem Read More »

Contains Duplicate solution using JavaScript

Contains Duplicate | Leetcode problem

In this post, I would like to talk about one of the most easiest problem you can see in the leetcode. In this problem, we have to tell whether an array contains duplicate value or not. The link to this question is here. Note that, the given array is all integer. Below I will give a short explanation of the leetcode problem solution.

Thought process to solve the problem

Whenever, I face a situation where I have to find if there is any duplicate or not (we are not finding out which one is the duplicate one, just if there is any identical value or not), all I do is to check the length of the array at first. Then I convert the array to a set. Note that a set can not contain any repeating values. Once it is converted to a set, I just check if the length of the set is same as the array or not. if yes, then there is no duplicate, if no, that means there is some duplicated value.

I have added the the JavaScript version of my solution.

Full Contains Duplicate Solution

var containsDuplicate = function(nums) {
    const uniqueSet = new Set(nums);
    
    if(uniqueSet.size === nums.length) {
        return false;
    }
    return true;
};

Obviously there are a lot of ways to solve the “Contains duplicate” problem. Like maybe you can use a loop (like, for, while etc) to check if there is any repeating values. But it will not be efficient. As for this kind of problem we should focus on finding the best solution.

Thanks again. In the next post, I will talk about another leetcode problem solution called Valid Anagram using JavaScript. Stay tuned.

Contains Duplicate | Leetcode problem Read More »