DEV Community

Abhinav Yadav
Abhinav Yadav

Posted on

Leetcode Solution #4

1. Replace Elements with Greatest Element on Right Side

The Problem

Start by reading the description of the problem,

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

This is a leetcode easy and pretty basic question as it is written in the question we have to replace every element in the array with greatest element to the right side of those elements and we have to replace the last element with -1.

Refer the example for better understanding,

Image description

Approach

Our approach for this question is going to be pretty straightforward,

  1. We will initialise last element as maximum.

  2. Set the last element as -1, because there won't be anything in right of last element.

  3. Traverse the array from right to left and return the maximum elements.

Solution

Image description

We have achieved the solution in following steps,

  1. Initialise max variable to last element of array. This will track for the maximum element when we traverse from right to left.

  2. Set last element as -1 as there are no elements right to it.

  3. Traverse from second last element because last has already been set to -1.

  4. Store max into a temporary variable temp because we need to update it.

  5. If current element is greater than max update the value to current value.

  6. Current element is replaced with temp, which held value of max element.

  7. Return arr.

2. Is Subsequence

The Problem

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

This question is also pretty basic and straightforward you have to check if string s is subsequence of t. If it is return true else return false.

Refer to example for better understanding,

Image description

Approach

The approach for this question is also not that special, we just have to keep check of some base and recursive cases, lets jump to the solution its very easy to understand this question through solution.

Solution

Image description

We approached the solution by following steps,

  1. Declare a helper function subsequence first with parameters string s and t (we have to compare between these strings) and variable m and n as length of these strings.

  2. Check for base cases if s is traversed completely i.e. m==0, return true because it means we have matched all the characters of s.

  3. If t is completely traversed return false because s still have unmatched characters.

  4. If last characters of s and t are equal move both m and n to next character.

  5. If current character mismatch skip current character in t by reducing n and keep m unchanged, checking if s can still be a subsequence of the remaining part of t.

  6. Now in main function isSubsequence if length of m>n return false.

  7. Otherwise, call the recursive Subsequence function to determine if s is a subsequence to t.

3. Length of Last Word

The Problem

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

This question is also leetcode easy and pretty basic, all we have to do is just to find the length of last word.

Refer the example for the better understanding,

Image description

Approach
We can approach this question in following way:

  1. Traverse the string from last character.

  2. Check if the last character is alphabetical or not.

  3. Return the length.

Solution

Image description

We reached this solution by following steps,

  1. Declare variable count that will hold length of last word.

  2. Declare a bool flag to track when we start counting characters of last word. It will be initialised as false.

  3. Iterate from right to left.

  4. Use if condition to check if current character is alphabet or not.

  5. When we encounter a letter flag turns to true indicating that we are counting.

  6. Increment count.

  7. If we encounter space after starting last word it means we have finished counting, return the count.

  8. If no space encountered after last word, return count.

I hope the solutions I have provided are understandable and explanations are easy to understand.

Thank You

You can connect with me on Linkedin

Top comments (0)