Remove All Islands Surrounded by Water in Python

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

742 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

186 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

461 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

Query for Ancestor-Descendant Relationship in a Tree in C++

sudhir sharma
Updated on 22-Dec-2020 08:48:39

275 Views

In this problem, we are given an N vertex tree and Q queries each consisting of two values i and j. Our task is to create a program to solve a Query for ancestor-descendant relationship in a tree.To solve each query, we need to check whether the node i is the ancestor of node j in the tree.Let’s take an example to understand the problem, InputQ = 2, query[][] = {{3, 5}, {1, 6}}OutputNo YesExplanationi = 3, j = 5: The node 3 is not the ancestor of node 5. Return NO. i = 1, j = 6: The node ... Read More

Evaluate S-Expression as String in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:47:31

544 Views

Suppose we have a string s as S-expression. We have to evaluate that S-expression and return result as integer. As we know that the s-expression is an expression which is either one number, or a recursive expression wrapped in parentheses like (+ (- 3 2) (* 3 3)), which indicates (3 - 2) + (3 * 3) = 10. Here valid operators are +, -, *, and /.So, if the input is like s = "(- (+ 3 2) 2)", then the output will be 3, as ((3 + 2) - 2) = 3.To solve this, we will follow these ... Read More

Update Index and Find GCD in Range in C++

sudhir sharma
Updated on 22-Dec-2020 08:46:39

269 Views

In this problem, we are given an array arr[] of size N and Q queries which can be of two types. Our task is to create a program to solve the queries to update a given index and find GCD in the range.Queries are −Type 1 − {1, index, value} - increase the element at given index by value.Type 2 − {2, L, R} - find the GCD of elements in the index range [L, R].Problem Description − We need to find the GCD of the elements that are in the range [L, R] and return the value.Let’s take an ... Read More

Check Character Replacement to Transform String in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:45:22

202 Views

Suppose we have two lowercase strings s and t. Now consider an operation where we replace all occurrences of a character in s with another character. If we can perform this operation any number of times, we have to check whether s can be converted to t or not.So, if the input is like s = "eye" t = "pip", then the output will be True, as we can replace "e"s with "p"s then "y" by "i".To solve this, we will follow these steps −Define one map m1 and another map m2n := size of sfor initialize i := 0, ... Read More

Absolute Difference Between L-th and R-th Smallest Number in C++

sudhir sharma
Updated on 22-Dec-2020 08:44:35

138 Views

In this problem, we are given an array arr[] of size n and Q queries each consisting of 2 values L and R. Our task is to create a program to solve queries to return the absolute difference between L-th smallest number and the R-th smallest number.Problem Description − To solve each query, we need to find the index of Lth smallest and Rth smallest number. And find the difference between these indices.Let’s take an example to understand the problem, Inputarr[] = {8, 4, 1, 5, 2} Q = 2 Queries[][] = {{2, 4}, {1, 5}}Output1 2ExplanationFor {2, 4}: 2nd ... Read More

Find Number of Operations to Remove Palindromic Sublists in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:43:21

273 Views

Suppose we have a list of numbers called nums. Now let us consider an operation where we delete some sublist which is a palindrome. We have to find the minimum number of operations required such that the list is empty.So, if the input is like nums = [6, 2, 4, 4, 2, 10, 6], then the output will be 2, as we can remove the sublist [2, 4, 4, 2] first then the list is like [6, 10, 6] this is also a palindrome, so remove it to make list empty.To solve this, we will follow these steps −Define an ... Read More

Check Regular Expression Pattern Matching with String in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:41:32

582 Views

Suppose we have a string s and a regular expression pattern. We have to check whether the given pattern matches with given string or not. In the regular expression, there are few rules −. (period) which matches any single character* (asterisk) which matches zero or more of the preceding element.So, if the input is like pattern = "h.l*o" s = "hello", then the output will be True, as We have ra and then a single characterTo solve this, we will follow these steps −n := size of sm := size of pDefine a function dp() . This will take i, ... Read More

Advertisements