Found 10783 Articles for Python

Find the longest sub-string which is prefix, suffix and also present inside the string in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:47:16

625 Views

Suppose we have a given string, we have to find the largest sub-string which is a prefix, a suffix and a sub-string of that given string. If there is no such substring, then return -1.So, if the input is like "languagepythonlanguageinterestinglanguage", then the output will be "language"To solve this, we will follow these steps −Define a function get_lps() . This will take stringn := size of stringlong_pref_suff := an array of size n, and fill with 0size := 0, long_pref_suff[0] := 0, i := 1while i < n is non-zero, doif string[i] is same as string[size], thensize := size + ... Read More

Find the lexicographically smallest string which satisfies the given condition in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:44:51

355 Views

Suppose we have an array A of n numbers, where A[i] indicates the number of distinct characters in the prefix of length (i + 1) of a string s, we have to find the lexicographically smallest string that satisfies the given prefix array. All characters will be lowercase English alphabets [a-z]. If there is no such string then return -1.So, if the input is like A = [1, 1, 2, 3, 4], then the output will be aabcd as prefix[0] has 1 distinct character, prefix[1] has 1 distinct character, prefix[2] has 2 distinct characters, prefix[3] has 3 distinct characters, prefix[4] ... Read More

Find the lexicographically largest palindromic Subsequence of a String in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:40:13

249 Views

Suppose we have a string S; we have to find the lexicographically largest palindromic subsequence of that string.So, if the input is like "tutorialspointtutorial", then the output will be "uu"To solve this, we will follow these steps −ans := blank stringmax_val := s[0]for i in range 1 to size of s, domax_val := maximum of max_val, s[i]for i in range 0 to size of s, doif s[i] is same as max_val, thenans := ans + s[i]return ansExample Let us see the following implementation to get better understanding − Live Demodef largest_palindromic_substr(s):    ans = ""    max_val = s[0]    for i ... Read More

Find the largest rectangle of 1’s with swapping of columns allowed in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:34:45

113 Views

Suppose we have a binary matrix, we have to find the largest rectangle of all 1's in that given matrix. The rectangle can be built by swapping or exchanging any pair of columns of that matrix.So, if the input is like100101001111010then the output will be the 6 in this case. The rectangle can be generating by exchanging column 1 with 3. The matrix after exchanging will be −001100011110110To solve this, we will follow these steps −row := size of matcol := size of mat[0]temp := a matrix of order (row + 1) x (col + 1), and fill with 0for ... Read More

Find the largest Perfect Subtree in a given Binary Tree in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:31:46

583 Views

Suppose we have a given Binary Tree; we have to find the size of largest Perfect sub-tree in that given Binary Tree. As we know the perfect binary tree is a binary tree in which all internal nodes have two children and all leaves are at the identical level.So, if the input is likethen the output will be 3, and the subtree isTo solve this, we will follow these steps −Define one block called RetType, this will hold isPerfect, height and rootTree, they are all initially 0Define a function called get_prefect_subtree(), this takes rootr_type := a new RetTypeif root is ... Read More

Find the index which is the last to be reduced to zero after performing a given operation in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:52:28

82 Views

Suppose we have an array A with n numbers and another input K, we have to find the index which will be the last to be reduced to zero after performing a given operation. The operation is explained as follows −Starting from A[0] to A[N – 1], update each element as A[i] = A[i] – K. Now, if A[i] < K then put A[i] = 0 and no further operation will be done on A[i] once it is 0.We have to repeat the operation until all of the elements are reduced to 0. And return the index which will be ... Read More

Find the element before which all the elements are smaller than it, and after which all are greater in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:47:51

365 Views

Suppose we have an array, we have to find an element before which all elements are less than it, and after which all are greater than it. Finally, return the index of the element, if there is no such element, then return -1.So, if the input is like A - [6, 2, 5, 4, 7, 9, 11, 8, 10], then the output will be 4.To solve this, we will follow these steps −n := size of arrmaximum_left := an array of size nmaximum_left[0] := -infinityfor i in range 1 to n, domaximum_left[i] := maximum of maximum_left[i-1], arr[i-1]minimum_right := infinityfor i ... Read More

Find the distance covered to collect items at equal distances in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:45:08

59 Views

Suppose one race is going to be organized. Where different stones are placed on a road. One bucket is present at the starting point of the race, this is 6 units away from the first stone. The other stones are 4 units apart from each other and lie straight in a line one after another. Now, the participants start from the bucket, then collects the nearest stone, comes back and puts that stone into the bucket, after that runs again to collect the next nearest stone, runs back, and puts it in the bucket. This process will be continued until ... Read More

Find the count of sub-strings whose characters can be rearranged to form the given word in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:41:47

172 Views

Suppose we have a string S (all letters are in lowercase), we have to find the count of all of the sub-strings of length four whose characters can be rearranged to form this word "bird".So, if the input is like "birdb", then the output will be 2.To solve this, we will follow these steps −cnt := 0for i in range 0 to size of s - 3, dobird := an array with [0, 0, 0, 0]for j in range i to i + 4, doif s[j] is same as 'b', thenbird[0] := bird[0] + 1otherwise when s[j] is same as ... Read More

Find the count of palindromic sub-string of a string in its sorted form in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:39:50

134 Views

Suppose we have a string of lowercase characters (all are ASCII characters), we have to find all distinct continuous palindromic sub-strings of the given string.So, if the input is like "level", then the output will be 7 as there are seven substrings ['level', 'eve', 'l', 'e', 'v', 'e', 'l'].To solve this, we will follow these steps −N := 26n := length of strsum := 0my_map := a list of size N and fill with 0for i in range 0 to n, domy_map[ASCII of (str[i]) - ASCII of ('a') ] := my_map[ASCII of (str[i]) - ASCII of ('a') ] + 1for ... Read More

Advertisements