Posts

Showing posts with the label leetcode

LeetCode - Best Time to Buy and Sell Stock Solution - The Coding Shala

Home >> LeetCode >> Best Time to Buy and Sell Stock  In this post, we will learn how to solve LeetCode's Best Time to Buy and Sell Stock problem and will implement its solution in Java. Best Time to Buy and Sell Stock Problem You are given an array of prices where prices[i] is the price of a given stock on an ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. Practice this problem on LeetCode . LeetCode - Best Time to Buy and Sell Stock Java Solution Approach 1 Straight forward solution. Java Program:  class ...

LeetCode - Same Tree Solution - The Coding Shala

Home >> LeetCode >> Same Tree  In this post, we will learn how to solve LeetCode's Same Tree problem and will implement its solution in Java. Same Tree Problem Given the roots of two binary trees p and q, write a function to check if they are the same or not.  Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Practice this problem on LeetCode . LeetCode - Same Tree Java Solution Approach 1 Using recursion. Java Program:  /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSameTree ( TreeNode p , TreeNode q ) { // using...

LeetCode - Check if Word Equals Summation of Two Words Solution - The Coding Shala

Home >> LeetCode >> Check if Word Equals Summation of Two Words  In this post, we will learn how to solve LeetCode's Check if Word Equals Summation of Two Words problem and will implement its solution in Java. Check if Word Equals Summation of Two Words Problem The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.). The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer.  For example, if s = "acb", we concatenate each letter's letter value, resulting in "021". After converting it, we get 21. You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through 'j' inclusive. Return true if the summation of the numerical values of firstWord and secondWord equals t...

Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts LeetCode Solution - The Coding Shala

Home >> LeetCode >> Maximum area of a piece of cake after horizontal and vertical cuts  In this post, we will learn how to solve LeetCode's Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts Problem and will implement its solution in Java. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts Problem Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7. Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 ...

Max Area of Island LeetCode Solution - The Coding Shala

Home >> LeetCode >> Max Area of Island  In this post, we will learn how to solve LeetCode's Max Area of Island Problem and will implement its solution in Java. Max Area of Island Problem You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. The area of an island is the number of cells with a value of 1 in the island. Return the maximum area of an island in the grid. If there is no island, return 0. Example 1: Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] Output: 6 Explanation: The answer is not 11, because the island must be connected 4-directionally. Practice this problem on LeetCode . LeetCode - Max Area of...

Replace All Digits with Characters LeetCode Solution - The Coding Shala

Home >> LeetCode >> Replace All Digits with Characters  In this post, we will learn how to solve LeetCode's Replace All Digits with Characters Problem and will implement its solution in Java. Replace All Digits with Characters Problem You are given a 0-indexed string s that has lowercase English letters in its even indices and digits in its odd indices. There is a function shift(c, x), where c is a character and x is a digit, that returns the xth character after c. For example, shift('a', 5) = 'f' and shift('x', 0) = 'x'. For every odd index i, you want to replace the digit s[i] with shift(s[i-1], s[i]). Return s after replacing all digits. It is guaranteed that shift(s[i-1], s[i]) will never exceed 'z'. Example 1: Input: s = "a1c1e1" Output: "abcdef" Explanation: The digits are replaced as follows: - s[1] -> shift('a',1) = 'b' - s[3] -> shift('c',1) = 'd' - s[5] -...

Goal Parser Interpretation LeetCode Solution - The Coding Shala

Home >> LeetCode >> Goal Parser Interpretation  In this post, we will learn how to solve LeetCode's Goal Parser Interpretation Problem and will implement its solution in Java. Goal Parser Interpretation Problem You own a Goal Parser that can interpret a string command. The command consists of an alphabet of "G", "()", and/or "(al)" in some order. The Goal Parser will interpret "G" as the string "G", "()" as the string "o", and "(al)" as the string "al". The interpreted strings are then concatenated in the original order. Given the string command, return the Goal Parser's interpretation of the command. Example 1: Input: command = "G()(al)" Output: "Goal" Explanation: The Goal Parser interprets the command as follows: G -> G () -> o (al) -> al The final concatenated result is "Goal". Example 2: Input: command = "G()()()()(al)...

Count Items Matching a Rule LeetCode Solution - The Coding Shala

Home >> LeetCode >> Count Items Matching a Rule  In this post, we will learn how to solve LeetCode's Count Items Matching a Rule Problem and will implement its solution in Java. Count Items Matching a Rule Problem You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue. The ith item is said to match the rule if one of the following is true: ruleKey == "type" and ruleValue == typei. ruleKey == "color" and ruleValue == colori. ruleKey == "name" and ruleValue == namei. Return the number of items that match the given rule. Example 1: Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver" Output: 1 Ex...

Check if the Sentence Is Pangram LeetCode Solution - The Coding Shala

Home >> LeetCode >> Check if the Sentence is Pangram  In this post, we will learn how to solve LeetCode's Check if the Sentence Is Pangram Problem and will implement its solution in Java. Check if the Sentence Is Pangram Problem A pangram is a sentence where every letter of the English alphabet appears at least once. Given a string sentence containing only lowercase English letters, return true if the sentence is a pangram, or false otherwise. Example 1: Input: sentence = "thequickbrownfoxjumpsoverthelazydog" Output: true Explanation: sentence contains at least one of every letter of the English alphabet. Example 2: Input: sentence = "leetcode" Output: false Practice this problem on LeetCode . LeetCode - Check if the Sentence Is Pangram Java Solution Approach 1 As we know HashSet contains only unique values. So here we can use a Character HashSet and then check the size of the set if it is 26 then the string is a pangram. Java Program:  c...

Sorting the Sentence LeetCode Solution - The Coding Shala

Home >> LeetCode >> Sorting the Sentence  In this post, we will learn how to solve LeetCode's Sorting the Sentence Problem and will implement its solution in Java. Sorting the Sentence Problem A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters. A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence. For example, the sentence "This is a sentence" can be shuffled as "sentence4 a3 is2 This1" or "is2 sentence4 This1 a3". Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original sentence. Example 1: Input: s = "is2 sentence4 This1 a3" Output: "This is a sentence" Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers. Example 2: Input:...

Maximum Population Year LeetCode Solution - The Coding Shala

Home >> LeetCode >> Maximum Population Year  In this post, we will learn how to solve LeetCode's Maximum Population Year Problem and will implement its solution in Java. Maximum Population Year Problem You are given a 2D integer array logs where each logs[i] = [birthi, deathi] indicates the birth and death years of the ith person. The population of some year x is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1]. Note that the person is not counted in the year that they die. Return the earliest year with the maximum population. Example 1: Input: logs = [[1993,1999],[2000,2010]] Output: 1993 Explanation: The maximum population is 1, and 1993 is the earliest year with this population. Example 2: Input: logs = [[1950,1961],[1960,1971],[1970,1981]] Output: 1960 Explanation: The maximum population is 2, and it had happened in years 1960 and 1970. The earlier year betw...