Suppose we have a string s, we have to find the number of non-empty unique subsequences of s. If the answer is very large then mod the result by 10^9 + 7.So, if the input is like s = "xxy", then the output will be 5, as there are five subsequences: "x", "xx", "xy", "y" and "xxy".To solve this, we will follow these steps −m := 10^9 + 7n := size of sDefine an array table of size 26res := 0for initialize i := 1, when i
Suppose we have a 2d binary matrix, we have to find the number of distinct islands in the given matrix. Here 1 represents land and 0 represents water, so an island is a set of 1s that are close and whose perimeter is surrounded by water. Here two islands are unique if their shapes are different.So, if the input is like100001010101101001001000011011then the output will be 4 (distinct islands are in different color).To solve this, we will follow these steps −Define a function dfs() . This will take i, j, k, lmat[i, j] := 0insert pair (i − k, j − ... Read More
Suppose we have a strings s. The s is containing digits from 0 - 9 and we also have another number k. We have to find the number of different ways that s can be represented as a list of numbers from [1, k]. If the answer is very large then return result mod 10^9 + 7.So, if the input is like s = "3456" k = 500, then the output will be 7, as we can represent s like [3, 4, 5, 6], [34, 5, 6], [3, 4, 56], [3, 45, 6], [34, 56], [345, 6], [3, 456]To solve ... Read More
Suppose we have a 2D matrix with 3 possible values −0 for an empty cell.1 for a coin.−1 for a wall.We have to find the maximum number of coins we can take by starting from the top−left cell and reaching the bottom−right cell by moving only right or down direction. Then come back to the top−left cell by only moving up or left direction. When we pick up a coin, the cell value becomes 0. If we cannot reach the bottom−right cell, then return 0.So, if the input is like011111−111011then the output will be 8.To solve this, we will follow ... Read More
Suppose two players are playing a game. Where several candies placed on a line, and person 1 is given a list of numbers called nums that is representing the point value of each candy. On each person's turn, they can pick 1, 2, or 3 candies from the front of the line, then delete them from list, and get the sum of their points added to their score. This game will end when all the candies are deleted and the person with the higher score will be the winner. We have to check person 1 can win this game or ... Read More
Suppose we have a list of numbers called candies and two persons are racing to collect the most number of candies. Here the race is turn based, person 1 is starting first, and in each turn he can pick up candies from the front or from the back. We have to check whether person 1 can collect more candies than other or not.So, if the input is like candies = [1, 4, 3, 8], then the output will be True, as person 1 can take 8 candies in the first round and regardless of whether second person picks 1 or ... Read More
Suppose we have a list of numbers called nums. We have to find the number of permutations of nums such that sum of every pair of adjacent values is a perfect square. Two permutations A and B are unique when there is some index i where A[i] is not same as B[i].So, if the input is like nums = [2, 9, 7], then the output will be 2, as we have [2, 7, 9] and [9, 7, 2]To solve this, we will follow these steps −res := 0Define a function util() . This will take iif i + 1 is ... Read More
Suppose we have two strings s, t, and another string r we have to check whether there is any way to get r by merging characters in order from s and t.So, if the input is like s = "xyz" t = "mno" r = "xymnoz", then the output will be True, as xymnoz can be formed by interleaving xyz and mno.To solve this, we will follow these steps −Define a function solve() . This will take s, t, rif s, t and r are empty, thenreturn Trueif r is empty, thenreturn Falseif s is empty, thenreturn true when t ... Read More
Suppose we have a lowercase string s with letters x and y. Now consider an operation in which we change a single x to y or vice versa. We have to find the minimum number of times we need to perform that operation to set all x's come before all y's.So, if the input is like s = "yxyyyyxyxx", then the output will be 4.To solve this, we will follow these steps −y_left := 0x_right := number of "x" in s, res := number of "x" in sfor each item in s, doif item is same as "x", thenx_right := ... Read More
Suppose we have a 2D matrix with few different values like below −0 for empty cell1 for a person2 for fire3 for a wallNow assume there is only one person and in each turn the fire expands in all four directions (up, down, left and right) but fire cannot expand through walls. We have to check whether the person can move to either the top-left corner or the bottom-right corner or the matrix. We have to keep in mind that in each turn, the person moves first, then the fire expands. If the person makes it to any of the ... Read More