Found 26504 Articles for Server Side Programming

Program to find minimum digits sum of deleted digits in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:19:53

171 Views

Suppose we have two strings s and t of digits, we have to find a way to remove digits in the strings so that: 1. Two strings are same 2. The sum of the digits that are deleted is minimized Finally return the minimized sum.So, if the input is like s = "41272" t = "172", then the output will be 6, as we can remove "4" and "2" from the first string to get "172".To solve this, we will follow these steps −Define a function lcs() . This will take a, b, m, ntable := a 2d matrix of ... Read More

Program to find minimum string size that contains given substring in Python

Arnab Chakraborty
Updated on 21-Dec-2020 14:08:50

411 Views

Suppose we have two strings s and t, we have to find the size of a minimum substring in s that contains all the characters of t. If there is no such substring exists then return -1.So, if the input is like s = "thegrumpywizardmakes" t = "wake", then the output will be 10, as the shortest substring that contains "wake" is "wizardmake" (length of 10).To solve this, we will follow these steps −counter := frequency of each character in bstart := 0min_subs := infrem := count of distinct characters in bfor end in range 0 to size of a, ... Read More

Program to count number of minimum swaps required to make it palindrome in Python

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

402 Views

Suppose we have a string s, we have to find the minimum number of adjacent swaps needed to make it into a palindrome. If there is no such way to solve, then return -1.So, if the input is like s = "xxyy", then the output will be 2, as we can swap the middle "x" and "y" so string is "xyxy" and then swap the first two "x" and "y" to get "yxxy", and this is palindrome.To solve this, we will follow these steps −Define a function util() . This will take sseen := a new mapfor each i in ... Read More

Program to find minimum possible difference of indices of adjacent elements in Python

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

255 Views

Suppose we have a list of numbers nums, we can say that two numbers nums[i] ≤ nums[j] are adjacent when there is no number in between (nums[i], nums[j]) in nums. We have to find the minimum possible |j - i| such that nums[j] and nums[i] are adjacent.So, if the input is like nums = [1, -9, 6, -6, 2], then the output will be 2, as we can see that 2 and 6 are adjacent and they are 2 indices away from each other.To solve this, we will follow these steps −indexes := a new mapfor each index i and ... Read More

Program to count number of words we can generate from matrix of letters in Python

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

275 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

Program to find intervals by merging target interval in Python

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

164 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]

Program to find list of elements which are less than limit and XOR is maximum in Python

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

190 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

Program to find length of subsequence that can be removed still t is subsequence of s in Python

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

211 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

Program to find maximum possible value of smallest group in Python

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

283 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

Program to find how max score we can get by removing 10 or 01 from binary string in Python

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

371 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

Advertisements