Check If Final String Can Be Formed Using Two Strings in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:38:50

161 Views

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

Count Flips to Make All X Before Y in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:37:17

193 Views

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

Check Reachability of Cell Avoiding Fire in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:34:04

141 Views

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

Count Walls Required to Partition Top-Left and Bottom-Right Cells in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:31:50

232 Views

Suppose we have a 2d binary matrix where 0 represents empty cell and 1 represents a wall. We have to find the minimum number cells that need to become walls so that there will be no path between top−left cell and bottom-right cell. We cannot put walls on the top−left cell and the bottom−right cell. We can move only left, right, up and down not diagonally.So, if the input is like0000010001100000then the output will be 2, 0100010001100010To solve this, we will follow these steps −R := row count of matrix, C := column count of matrixvisited := a new settin ... Read More

Find Number of Unique Subsequences Same as Target in C++

Arnab Chakraborty
Updated on 25-Dec-2020 06:03:39

161 Views

Suppose we have two lowercase strings s and t, we have to find the number of subsequences of s that are equal to t. If the answer is very large then return result by 10^9 + 7.So, if the input is like s = "abbd" t = "bd", then the output will be 2, as there are two possible subsequences "bd".s[1] concatenate s[3]s[2] concatenate s[3].To solve this, we will follow these steps −m := 10^9 + 7if size of t is same as 0, then −return 0if t is same as s, then −return 1if size of t > size ... Read More

Count Distinct Characters of Every Substring in Python

Arnab Chakraborty
Updated on 25-Dec-2020 06:01:45

317 Views

Suppose we have a lowercase string s, we have to find the sum of the count of characters that are distinct in every substring of s. If the answer is very large then return result mod 10^9+7.So, if the input is like s = "xxy", then the output will be 6, as the substrings and their counts are −"x" : 1"x" : 1"y" : 1"xx" : 0 (as "x" is not distinct)"xy" : 2"xxy" : 1 ("as "x" is not distinct)To solve this, we will follow these steps −m := 10^9 + 7prev_seen := a new empty mapans := 0Define ... Read More

Find Number of Operations to Convert List into Non-Increasing List in Python

Arnab Chakraborty
Updated on 25-Dec-2020 06:00:33

161 Views

Suppose we have a list of numbers called nums. Now let us consider an operation where we take two consecutive values and merge it into one value by taking their sum. We have to find the minimum number of operations required so that the list turns into non−increasing.So, if the input is like nums = [2, 6, 4, 10, 2], then the output will be 2, as we can merge [2, 6] to get [8, 4, 10, 2] and then merge [8, 4] to get [12, 10, 2].To solve this, we will follow these steps −if nums is empty, thenreturn ... Read More

Count Configurations to Fill Area with Dominos and Trominos in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:58:00

113 Views

Suppose we have two shapes, Domino and Tromino. Dominos are 2 x 1 shape and Trominos are ‘L’ like shape. They can be rotated like below −If we have a number n, we have to find number of configurations to fill a 2 x n board with these two types of pieces. As we know in tiling, every square must be covered by a tile.So if the input is 3, then the output will be 5. So the arrangements can be [XYZ XXZ XYY XXY XYY] and [XYZ YYZ XZZ XYY XXY], here different letters are used for different tiles.To ... Read More

Count Trailing Zeros of Minimum Number Divisible by 1 to K in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:56:38

154 Views

Suppose we have a number k, now consider the smallest positive integer value x where all values from 1 to k divide evenly. In other words, consider the smallest value x where x is divisible by all numbers from 1 through k. We have to find the number of trailing zeroes in x.So, if the input is like k = 6, then the output will be 0, as the smallest x here is 60, 60 can be divided using 1, 2, 3, 4, 5 and 6. There is only one trailing zeroes in 60.To solve this, we will follow these steps −res := 0x := 1while x * 5

Find Top View of a Binary Tree in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:55:52

358 Views

Suppose we have a binary tree, we have to find the top view of the tree, they will be sorted left−to−right.So, if the input is like image, then the output will be [3, 5, 8, 6, 9], as 3 is above 2 and 5 is above 7 so they are not visible.To solve this, we will follow these steps −view := a new empty mapq := a double ended queueinsert pair (root, 0) at the end of qstart := inf, end := −infwhile q is not empty, do(node, coord) := left element of q, then remove left element of qstart ... Read More

Advertisements