Count Words Generated from Matrix of Letters in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:17:50

272 Views

Suppose we have a 4 x 4 board of letters and a list of words, we have to find the largest number of words that can be generated in the board by a sequence of adjacent letters, using one cell at most once per word (but we can reuse cells for other words). We can go up, down, left, right, or diagonal direction.So, if the input is likembfdxayatztrsqqqwords = ["bat", "far", "mat"], then the output will be 3, as we can generate mat [0, 1] → [1, 1] → [2, 0], bat [0, 2] → [1, 1] → [2, 2], ... Read More

Find Intervals by Merging Target Interval in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:16:58

163 Views

Suppose we have a list of non-overlapping intervals. These are sorted based on end time. We have another interval target, find final interval after merging target so that intervals are still non-overlapping and sorted.So, if the input is like intervals = [[1, 15],[25, 35],[75, 90]], target = [10, 30], then the output will be [[1, 35], [75, 90]] as first two intervals [1, 15] and [25, 35] are merged.To solve this, we will follow these steps −insert target at the end of ivsort iv based on start timeres := a new list with first intervali := 1while i < size of iv, doif start time of iv[i]

Find Elements Less Than Limit with Maximum XOR in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:16:11

189 Views

Suppose we have a list of numbers nums and a list of queries where each query contains [x, limit]. We have to find a list such that for each query [x, limit], we find an element e in nums such that e ≤ limit and e XOR x is maximized. If there is no such element, return -1.So, if the input is like nums = [3, 5, 9] queries = [[4, 6], [2, 0]], then the output will be [3, -1], as for the first query, we can use 2 or 4 in nums. 3 ^ 4 = 7 while ... Read More

Find Length of Removable Subsequence in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:15:18

210 Views

Suppose we have a string s and another string t. And t is a subsequence of s. We have to find the maximum length of a substring that we can remove from s so that, t is still a subsequence of s.So, if the input is like s = "xyzxyxz" t = "yz", then the output will be 4, as We can remove the substring "abca"To solve this, we will follow these steps −left := a new list, right := also a new listc1 := -1, c2 := -1, c3 := -1j := 0for i in range 0 to size ... Read More

Find Maximum Possible Value of Smallest Group in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:14:34

281 Views

Suppose we have a list of numbers called nums and another value k. We have to split the list into k contiguous groups. The smallest group is the one whose sum is the smallest out of all the groups. So find the maximum possible value of the smallest group.So, if the input is like nums = [2, 6, 4, 5, 8] k = 3, then the output will be 8, as we can split the list into three groups like: [2, 6], [4, 5], [8]. So the minimum group has sum 8.To solve this, we will follow these steps −Define ... Read More

Max Score by Removing 10 or 01 from Binary String in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:13:26

363 Views

Suppose we have a binary string s and two values zero_one and one_zero. Now let us consider an operation where we can delete any substring "01" and receive zero_one points. Or we can remove any substring "10" and receive one_zero points. We have to find the maximum number of points we can get after any number of operations.So, if the input is like s = "10100101" zero_one = 3 one_zero = 2, then the output will be 11, as we can remove "01" three times to get 3*3 = 9 points. Then the remaining string is 10. By removing this ... Read More

Find Triplet from List in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:57:29

298 Views

Suppose we have a list of numbers called nums, we have to check whether there are triplets (i, j, k) such that i < j < k and nums[i] < nums[k] < nums[j].So, if the input is like nums = [2, 12, 1, 4, 4], then the output will be True, as [2, 12, 4] matches the criteria because 2 < 4 < 12.To solve this, we will follow these steps −n := size of numsDefine an array left of size nleft[0] := nums[0]for initialize i := 1, when i < n, update (increase i by 1), do −left[i] := ... Read More

Remove All Islands Surrounded by Water in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:55:56

734 Views

Suppose we have a binary matrix where 1 represents land and 0 represents water. And an island is a group of 1's that are surrounded by 0s (water) or by the edges. We have to find all of the islands that are completely surrounded by water and modify them into 0s. As we know an island is completed surrounded by water if all of the neighbors (horizontal and vertical not diagonal) are 0s (none of the neighbors are edges).So, if the input is like10000110011001100001then the output will be10000000000000000001To solve this, we will follow these steps −row := row count of ... Read More

Find Length of Shortest Supersequence in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:52:38

174 Views

Suppose we have two strings s and t. We have to find the length of the shortest string that has both s and t as subsequences.So, if the input is like s = "pipe" t = "people", then the output will be 7, as one possible supersequence is "pieople".To solve this, we will follow these steps −m := size of s, n := size of ttable := a table of size (n + 1) x (m + 1) and fill with 0for i in range 0 to m, dofor j in range 0 to n, doif i is same as ... Read More

Find Distance of Shortest Bridge Between Islands in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:50:57

453 Views

Suppose we have a binary matrix, where 0 represents water and 1 represents the land. An island is a group of connecting 1s in 4 directions. Islands are either surrounded by 0s (water) or by the edges. We have to find the length of shortest bridge that connects two islands.So, if the input is like001101100then the output will be 1. This will connect (1, 0) to (1, 2) points.To solve this, we will follow these steps −row := row count of matrixcol := column count of matrixDefine a function dfs() . This will take i, j, sif (i, j) is ... Read More

Advertisements