Check First Player Win in String Game Using C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:32:10

189 Views

Suppose we have a list of words. Now consider a ghost game where two players can participate into it. Here players alternate appending letters to a string. And the string that is being made must be a valid prefix of a word in the list, and the player who spells out any word in the list loses. We have to check whether the first player can win or not if both players are playing optimally.So, if the input is like words = ["manage", "manager", "min"], then the output will be True, as they can play like −m [Player 1]ma [Player ... Read More

Check If We Can Cross River by Stones in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:29:42

363 Views

Suppose we have a list of sorted numbers called stones and this is representing the positions of stones on a river that we are trying to cross. To cross the river, we must finish at the last stone. Now in each step, we can jump (k - 1, k, or k + 1) steps ahead where k is the distance of the last jump. We have to check whether we can cross the river or not.So, if the input is like stones = [0, 1, 3, 4, 5, 6, 8, 9, 13], then the output will be True, as we ... Read More

Find Minimum Number of Colors After Merging in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:27:21

200 Views

Suppose we have a list of colors (R, G, B). Now if two different colors are there next to each other then they can transform into a single color item of the third color. We have to find the smallest number of them remaining after any possible sequence of such transformations.So, if the input is like colors = ["G", "R", "G", "B", "R"], then the output will be 1 as it can transform like below −To solve this, we will follow these steps −n := size of colorsif colors has only one distinct color, thenreturn nif n

Find Land with Longest Distance from Water in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:25:00

192 Views

Suppose we have a binary matrix where, 0 represents water and 1 represents land. Now we have to find the land which has the longest Manhattan distance from water and finally return the distance.So, if the input is like1111110111110011then the output will be 3, as [0, 0] cell has Manhattan distance of 3 from water.To solve this, we will follow these steps −if A is empty, thenreturn 0R := row count of matrix, C := column count of matrixdistance := a matrix of order R x C and fill with 0q := a double ended queue with some pairs (r, ... Read More

Convert One String to Another in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:22:27

217 Views

Suppose we have two strings S and T. We have to find the shortest sequence of operations that changes S to T. Here the operations are basically either deleting or inserting a character.So, if the input is like S = "xxxy" T = "xxyy", then the output will be ["x", "x", "-x", "y", "+y"], this means place first two x's, then remove 3rd x, then place y then add a new y.To solve this, we will follow these steps −make a table dp of size 505 x 505Define a function help(), this will take i, j, S, T, if i ... Read More

Count Ways to Fill 3 x n Box with 2 x 1 Dominos in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:18:55

254 Views

Suppose we have a number n, we have to find the number of ways we can fill a (3 x n) block with 1 x 2 dominos. We can rotate the dominos when required. If the answer is very large then return this mod 10^9 + 7.So, if the input is like n = 4, then the output will be 11.To solve this, we will follow these steps −m = 10^9 + 7if n is odd, thenreturn 0cs := 1, os := 0for i in range 2 to n, increase by 2, docs := 3 * cs + osos := ... Read More

Find Nth Term of a Sequence Divisible by A, B, C in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:17:05

379 Views

Suppose we have four numbers n, a, b, and c. We have to find the nth (0 indexed) term of the sorted sequence of numbers divisible by a, b or c.So, if the input is like n = 8 a = 3 b = 7 c = 9, then the output will be 18, as The first 9 terms of the sequence are [1, 3, 6, 7, 9, 12, 14, 15, 18].To solve this, we will follow these steps −if minimum of a, b, c is same as 1, thenreturn nab := lcm(a, b), bc := lcm(b, c), ca := ... Read More

Find Length of Largest Subset Where One Element is Divisible by Another in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:13:56

357 Views

Suppose we have a list of unique numbers called nums, so we have to find the largest subset such that every pair of elements in the subset like (i, j) satisfies either i % j = 0 or j % i = 0. So we have to find the size of this subset.So, if the input is like nums = [3, 6, 12, 24, 26, 39], then the output will be 4, as the largest valid subset is [3, 6, 12, 24].To solve this, we will follow these steps −dp := a list of size nums and fill with 1sort the list numsn := size of numsif n

Find Number of Sublists with Exactly K Different Words in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:11:43

119 Views

Suppose we have a list of words and another value k. We have to find the number of sublists in the given words such that there are exactly k different words.So, if the input is like words = ["Kolkata", "Delhi", "Delhi", "Kolkata"] k = 2, then the output will be 5, as the following sublists have 2 unique words: ["Kolkata", "Delhi"], ["Delhi", "Kolkata"], ["Kolkata", "Delhi", "Delhi"], ["Delhi", "Delhi", "Kolkata"], ["Kolkata", "Delhi", "Delhi", "Kolkata"], but not ["Delhi", "Delhi"] as there is only one unique word.To solve this, we will follow these steps −Define a function work() . This will take words, ... Read More

Find How Long It Will Take to Reach Messages in a Network in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:09:08

180 Views

Suppose we have a number and a list of edges. These n different nodes labeled as 0 to N. These nodes are forming a network. Each edge is in the form (a, b, t) of an undirected graph, this is representing if we try to send message from a to b or b to a, it will take t time. When a node receives a message, it immediately floods the message on to a neighboring node. If all nodes are connected, we have to find how long it will take for every node to receive a message that starts at ... Read More

Advertisements