Programming Articles - Page 1022 of 3363

Program to check whether two sentences are similar or not in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:39:15

1K+ Views

Suppose we have two sentences s and t. We have to check whether they are similar or not. Here sentence has only English letters. Two sentences are said to be similar when it is possible to add an arbitrary sentence (possibly empty) inside one of these given sentences such that the two sentences become equal.So, if the input is like s = "we live at city Kolkata" t = "city Kolkata", then the output will be True because we can get s from t by adding sentence "we live in".To solve this, we will follow these steps −s1 := a ... Read More

Program to find total sum of all substrings of a number given as string in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:36:02

526 Views

Suppose we have a number in string format, and we have to find the sum of all substrings of s. The answer may be very large, so return result modulo 10^9+7.So, if the input is like s = "268", then the output will be 378 as the substrings are "2", "6", "8", "26", "68" and "268" total sum is 378.To solve this, we will follow these steps −M := 10^9 + 7sum_val := 0B := 1res := 0for i in range size of s - 1 down to 0, decrease by 1, dores :=(res + digit value of s[i] * ... Read More

Program to find minimum remove required to make valid parentheses in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:34:13

447 Views

Suppose we have a string s with parenthesis '(' , ')' and lowercase English characters. We have to delete the minimum number of parentheses ( '(' or ')', from any positions ) so that the resulting parentheses string is valid and have to finally return any valid string. Here the parentheses string is valid when all of these criteria are fulfilled −The string is empty and contains lowercase characters only, orThe string can be written as AB (A concatenated with B), where A and B are valid strings, orThe string can be written as the form of (A), where A ... Read More

Program to find numbers with same consecutive differences in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:29:23

407 Views

Suppose we have to find an array of size N such that the absolute difference between every two consecutive digits is K. Every number in the answer must not have leading zeros except for the number 0 itself.So, if the input is like N = 4 K = 7, then the output will be [1818, 2929, 7070, 8181, 9292], here 0707 is not valid as it has leading 0.To solve this, we will follow these steps −if N is same as 1, thenreturn a new list from range 0 to 9queue := make a queue with all elements from 1 ... Read More

Program to find how many swaps needed to sort an array in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:32:03

2K+ Views

Suppose, we have an array called nums and we have to find the number of swaps needed to make nums sorted in any order, either ascending or descending.So, if the input is like nums = [2, 5, 6, 3, 4], then the output will be 2 because initially nums has [2, 5, 6, 3, 4]. If we swap numbers 6 and 4, the array will be [2, 5, 4, 3, 6]. Then, if we swap the numbers 5 and 3, the array will be [2, 3, 4, 5, 6]. So 2 swaps are needed to make the array sorted in ... Read More

Program to find maximum value at a given index in a bounded array in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:25:44

410 Views

Suppose we have three values, n, index and maxSum. Consider an array called nums we have to find nums[index] and nums satisfies the following conditions −size of nums is nAll elements in n is positive.|nums[i] - nums[i+1]|

Program to find partition array into disjoint intervals in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:18:32

294 Views

Suppose we have an array nums, we have to partition it into two different subarrays called left and right such that −Each element in left subarray is less than or equal to each element in right subarray.left and right subarrays are non-empty.left subarray has the smallest possible size.We have to find the length of left after such a partitioning.So, if the input is like nums = [5, 0, 3, 8, 6], then the output will be 3 because left array will be [5, 0, 3] and right subarray will be [8, 6].To solve this, we will follow these steps −mx ... Read More

Program to find maximum number of consecutive values you can make in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:14:35

483 Views

Suppose we have an array called coins with n elements, and it is representing the coins that we own. The value of the ith coin is denoted as coins[i]. We can make some value x if we can select some of our n coins such that their values sum up to x. We have to find the maximum number of consecutive values that we can get with the coins starting from and including 0.So, if the input is like coins = [1, 1, 3, 4], then the output will be 10, because0 = []1 = [1]2 = [1, 1]3 = ... Read More

Program to find out the number of accepted invitations in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:21:32

360 Views

Suppose there are m boys and n girls, and m = n. There is a party incoming, and each boy has to go with a girl to that party. So, the boys invite all the girls and a girl can accept one invitation only. We have to find out the total number of invitations from the boys that the girls can accept. The input is given in a m x n matrix, where each cell position i, j denotes if the boy i has sent a letter to girl j. If a cell is 1 it means that an invitation ... Read More

Program to find out the number of pairs of equal substrings in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:04:37

376 Views

Suppose we are given two strings, both made of lowercase alphabets. We have to find out the number of quadruples (p, q, r, s) satisfying the given conditions −0

Advertisements