Server Side Programming Articles - Page 1868 of 2650

Check If Word Is Valid After Substitutions in C++

Arnab Chakraborty
Updated on 02-May-2020 10:44:26

351 Views

Suppose we are given that the string "abc" is valid. So from any valid string V, we can divide V into two pieces X and Y such that X + Y is same as V. (X or Y may be empty.). Then, X + "abc" + Y is also valid. So for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". And some examples of invalid strings are: "abccba", "ab", "cababc", "bac". We have to check true if and only if the given string S is valid.So if the input is like “abcabcababcc”, then that ... Read More

Maximum Binary Tree II in C++

Arnab Chakraborty
Updated on 02-May-2020 10:43:36

210 Views

Suppose we have a root node of a maximum tree: The maximum tree is a tree where every node has a value greater than any other value in its subtree. Suppose we have a method called construct(). This can construct a root from a list A. The construct() method is like −If list A is empty, return null.Otherwise, let A[i] be the largest element of the list A. Then create a root node with value A[i].The left child of root will be construct([A[0], A[1], ..., A[i-1]])The right child of root will be construct([A[i+1], A[i+2], ..., A[n - 1]]) [n is ... Read More

Smallest String Starting From Leaf in Python

Arnab Chakraborty
Updated on 02-May-2020 10:35:23

286 Views

Suppose we have the root of a binary tree, each node is containing a value from 0 to 25 , which are representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. We have to search the lexicographically smallest string that starts at a leaf of this tree and finishes at the root. So if the tree is like −Then the output will be “adz” as the sequence is [0, 3, 25]To solve this, we will follow these steps −Define a dfs traversal method as followsif node is not ... Read More

Interval List Intersections in C++

Arnab Chakraborty
Updated on 02-May-2020 10:36:38

323 Views

Suppose we have two lists of closed intervals, here each list of intervals is pairwise disjoint and in sorted order. We have ti find the intersection of these two interval lists.We know that the closed interval [a, b] is denoted as a A[i][1] or A[i][1] B[j][1] or B[j][1] B[j][1]:          j+=1       else:          i+=1    return result    def intersect(self,a,b):       if a[0]=b[0]:          return True       if b[0]=a[0]:          return True       return False ob = Solution() print(ob.intervalIntersection([[0,2],[5,10],[13,23],[24,25]],[[1,5],[8,12],[15,24],[25,27]]))Input[[0,2],[5,10],[13,23],[24,25]] [[1,5],[8,12],[15,24],[25,27]]Output[[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]]

Minimum Cost For Tickets in C++

Arnab Chakraborty
Updated on 02-May-2020 10:27:40

503 Views

Suppose there is a country, that is popular for train travel, we have planned some train travelling one year in advance. We have an array, that is holding the days of the year that we will travel. Each day is an integer from 1 to 365. Train tickets are sold in three different ways −A 1-day pass is sold for costs[0] dollars;A 1-day pass is sold for costs[0] dollars;A 30-day pass is sold for costs[2] dollars.Here the passes allow that many days of consecutive travel. For example, if we get one 7-day pass on day 2, then we can travel ... Read More

Distribute Coins in Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 10:22:04

241 Views

Suppose we have the root of a binary tree with N nodes, here each node in the tree has node.val number of coins, and there are N coins in total. In one move, we can choose two adjacent nodes and move only one coin from one node to the another node. (The move may be from parent to child node, or from child to parent node.). We have to find the number of moves required to make every node have exactly one coin.So if the tree is like −Then the output will be 3. From the left child, send 2 ... Read More

Pancake Sorting in C++

Arnab Chakraborty
Updated on 02-May-2020 10:18:14

667 Views

Suppose we have an array A, we will perform the Pancake sort technique on A. Here the main constraint is that we can use only one operation called rev(arr, i). This will reverse the elements of arr from 0 to ith position. This idea is like the selection sort. We repeatedly place the max element at end reduce the size of the array. So if the input is like [54, 85, 52, 25, 98, 75, 25, 11, 68], then the result will be [11, 25, 25, 52, 54, 68, 75, 85, 98]To solve this, we will follow these steps −size ... Read More

Check Completeness of a Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 10:17:15

241 Views

Suppose we have a binary tree. We have to check whether the tree is a complete binary tree or not. A complete binary tree of level n, has n-1 complete levels, and all nodes at level n, are filled from the left. So if the input tree is like −Then the output will be true, As this is complete binary tree.To solve this, we will follow these steps −If tree is empty, then return nullmake a queue q and insert root into itset flag := truewhile q has some elementssz := size of the queuewhile sz is not 0node := ... Read More

Flip Equivalent Binary Trees in C++

Arnab Chakraborty
Updated on 02-May-2020 10:11:36

325 Views

Suppose we have a binary tree. We have to flip the binary tree. The flip indicates: choose any node, and swap the left and right child subtrees. Now a binary tree X is flip equivalent to a binary tree Y if and only if we can make Y from X, after some number of flip operations. We have to write a method that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2. So if the trees are −Then the output will be true, if we flip nodes with values 1, 3 ... Read More

Validate Stack Sequences in C++

Arnab Chakraborty
Updated on 02-May-2020 10:09:17

438 Views

Suppose we have two sequences pushed and popped with distinct values, we have to find true if and only if this could have been the result of a sequence of the push and pop operations on an initially empty stack. So if the input is push = [1, 2, 3, 4, 5], and pop = [4, 5, 3, 2, 1], then the output will be true. We can use push(1), push(2), push(3), push(4), pop() : 4, push(5), pop() : 5, pop() : 3, pop() : 2, pop() : 1To solve this, we will follow these steps −Create one method called ... Read More

Advertisements