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,
Approach
Our approach for this question is going to be pretty straightforward,
We will initialise last element as maximum.
Set the last element as -1, because there won't be anything in right of last element.
Traverse the array from right to left and return the maximum elements.
Solution
We have achieved the solution in following steps,
Initialise max variable to last element of array. This will track for the maximum element when we traverse from right to left.
Set last element as -1 as there are no elements right to it.
Traverse from second last element because last has already been set to -1.
Store max into a temporary variable temp because we need to update it.
If current element is greater than max update the value to current value.
Current element is replaced with temp, which held value of max element.
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,
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
We approached the solution by following steps,
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.
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.
If t is completely traversed return false because s still have unmatched characters.
If last characters of s and t are equal move both m and n to next character.
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.
Now in main function isSubsequence if length of m>n return false.
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,
Approach
We can approach this question in following way:
Traverse the string from last character.
Check if the last character is alphabetical or not.
Return the length.
Solution
We reached this solution by following steps,
Declare variable count that will hold length of last word.
Declare a bool flag to track when we start counting characters of last word. It will be initialised as false.
Iterate from right to left.
Use if condition to check if current character is alphabet or not.
When we encounter a letter flag turns to true indicating that we are counting.
Increment count.
If we encounter space after starting last word it means we have finished counting, return the count.
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)