Server Side Programming Articles - Page 1807 of 2650

Find and Replace Pattern in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:29:54

206 Views

Suppose we have a list of words and a pattern, and we have to find which words in words matches the pattern. Here a word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the target word. We have to find a list of the words in words that match the given pattern.So for example, if the input is like ["abc", "deq", "mee", "aqq", "dkd", "ccc"] and pattern is “abb”, then the output will be [“mee”, “aqq”], here mee and aqq are matching the ... Read More

Construct Binary Tree from Preorder and Postorder Traversal in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:14:19

515 Views

Suppose we have two traversal sequences Preorder and Postorder, we have to generate the binary tree from these two sequences. So if the sequences are [1, 2, 4, 5, 3, 6, 7], [4, 5, 2, 6, 7, 3, 1], then the output will beTo solve this, we will follow these steps −ans := make a tree node by taking value pre[0], stack := empty stack, and insert ansi := 1 and j := 0while i < length of pre and j < length of postif stack top value = post[j], then increase j by 1, pop from stack, and go ... Read More

Decoded String at Index in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:09:03

215 Views

Suppose one encoded string S is given. We have to find and write the decoded string to a tape, here the encoded string is read one character at a time and the following steps are performed −If the character read is a letter, that letter is simply written onto the tape.If the character read is a digit, the entire current tape is repeatedly written digit – 1 more times in total.Now if some encoded string S, and an index K is given, find and return the K-th letter (starting indices from 1) in the decoded string.So if the string is ... Read More

Most Profit Assigning Work in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:04:56

375 Views

Suppose we have jobs difficulty[i] and this array indicates the difficulty of the ith job, and profit[i] is the profit of the ith job. Now consider we have some workers. worker[i] is the ability of the ith worker, this means that this worker can only complete a job with difficulty at most worker[i]. Every worker can do at most one job, but one job can be completed multiple times. We have to find what is the most profit we can make?For example, if the input is like difficulty = [2, 4, 6, 8, 10] and profit = [10, 20, 30, ... Read More

Number of Matching Subsequences in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:00:45

341 Views

Suppose we have a string S and a dictionary of words words, find the number of words[i] that is a subsequence of S. So if the input is S= “abcde” and dictionary is [“a”, “bb”, “acd”, “ace”], then output will be 3. Because there are three sequence of words in the dictionary, that are a subsequence of S: “a” “acd” and “ace”To solve this, we will follow these steps −n := size of words arraycreate one map mfor i in range 0 to size of wordsinsert words[i] into the map m[words[i, 0]] positionans := 0for i in range 0 to ... Read More

Reorganize String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 05:57:20

662 Views

Suppose we have a string S, check whether the letters can be rearranged so that two characters that are adjacent to each other are not the same. If that is possible, output any possible result. If that is not possible, return the empty string. So if the input is like “AAB”, then the output will be “ABA”.To solve this, we will follow these steps −Make a priority queue of integer character pairs called pq, define one map mn := size of the stringstore the character frequency in map mfor each key-value pair p in minsert (integer part of p, character ... Read More

Kth Smallest Element in a Sorted Matrix in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:54:09

460 Views

Suppose we have a n x n matrix where each of the rows and columns are sorted in increasing order, we have to find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth unique element. So if the input is like [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k = 8, then the output will be 13.To solve this, we will follow these steps −define one method called checkVal() and the arguments are matrix and valuei := 0, j := length of matrix[0] – ... Read More

Guess Number Higher or Lower II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:25:59

318 Views

Suppose we are playing the Guess Game. The rules of the game is as follows −Player1 pick a number from 1 to n. player2 have to guess which number is picked by player1.Every time player2 guess wrong, player1 will tell whether the number that is picked is higher or lower.However, when a player guess a particular number x, and another player guess wrong, another player has to pay $x. The game will end, when player2 got the correct answer.For example if n = 10, and the player1 has taken 8In the first round, player2 tells the number is 5, that ... Read More

Find K Pairs with Smallest Sums in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:15:57

216 Views

Suppose we have two sorted arrays A1 and A2, and another value k. We have to define a pair (u, v) which is consists of one element from A1 and another element from A2. We have to find the k pairs like [(u1, v1), (u2, v2), …, (uk, vk)]. So if A1 = [1, 7, 11] and A2 = [2, 4, 6], and k = 3, then output will be [(1, 2), (1, 4), (1, 6)]To solve this, we will follow these steps −Define one data type, that will take two values a and b, and index.create one priority queue, ... Read More

Populating Next Right Pointers in Each Node II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:04:39

155 Views

Suppose we have a binary tree, where each node has following fields: (data, left, right, next), the left will point to left subtree, right will point to right subtree, and the next pointer will point to the next node. If there is no node in the right hand side, then that will be null. So initially each next pointer is set to null, we have to make the links. Suppose the tree is like the first one, it will be converted to the next node −To solve this, we will follow these steps −set pre := root, nextPre := null ... Read More

Advertisements