Found 10476 Articles for Python

Program to find length of longest sublist whose sum is 0 in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:41:36

139 Views

Suppose we have a list with only two values 1 and −1. We have to find the length of the longest sublist whose sum is 0.So, if the input is like nums = [1, 1, −1, 1, 1, −1, 1, −1, 1, −1], then the output will be 8, as the longest sublist is [−1, 1, 1, −1, 1, −1, 1, −1] whose sum is 0.To solve this, we will follow these steps −table := a new empty mapcs := 0, max_diff := 0for i in range 0 to size of nums − 1, docs := cs + nums[i]if cs ... Read More

Program to find length of longest substring which contains k distinct characters in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:39:26

752 Views

Suppose we have a number k and another string s, we have to find the size of the longest substring that contains at most k distinct characters.So, if the input is like k = 3 s = "kolkata", then the output will be 4, as there are two longest substrings with 3 distinct characters these are "kolk" and "kata", which have length 4.To solve this, we will follow these steps −ans := 0, left := 0table := a new mapfor right is in range 0 to size of s − 1, dotable[s[right]] := 1 + (s[right] if exists otherwise 0)if ... Read More

Program to find length of longest substring with even vowel counts in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:36:37

418 Views

Suppose we have a string s (lowercase), we have to find the length of the longest substring where each vowel occurs even number of times.So, if the input is like s = "anewcoffeepot", then the output will be 10, as the substring "wcoffeepot" has two vowels "o" and "e", both of which occurs two times.To solve this, we will follow these steps −vowels := a map assigning vowels and numeric values as {a:0, e:1, i:2, o:3, u:4}prefix := an empty map and insert a key-value pair (0, −1) into itmask := 0, n := size of s, res := 0for ... Read More

Program to find length of longest palindromic substring after single rotation in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:34:46

219 Views

Suppose we have a string s, which we can rotate at any point exactly once. We have to find the length of the longest palindromic substring we can get by doing this operation.So, if the input is like s = "elklev", then the output will be 7, as we can rotate between "el" and "klev" to get "levelk". So here the longest palinfromic substring length is 5.To solve this, we will follow these steps −s2 := concatenate s twicemax_len := 0for x in range 0 to size of s − 1, dofor y in range 0 to size of s, ... Read More

Program to find length of the longest path in a DAG without repeated nodes in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:47:33

1K+ Views

Suppose we have one directed acyclic graph represented by the adjacency list. We have to find the longest path in the graph without node repetition.So, if the input is likethen the output will be 4, as the path is 0 -> 1 -> 3 -> 4 -> 2 with length 4.To solve this, we will follow these steps −ans := 0n := node count of graphtable := a list of size n and fill with -1Define a function dfs() . This will take uif table[u] is not -1, thenreturn table[u]p_len := 0for each vectex v in graph[u], dop_len := maximum ... Read More

Program to find length of longest palindromic subsequence in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:45:08

850 Views

Suppose we have a lowercase string s; we have to find the length of the longest palindromic subsequence in s.So, if the input is like s = "aolpeuvekyl", then the output will be 5, as the palindrome is "level".To solve this, we will follow these steps −n := size of sDefine a function dp() . This will take i, jif i is same as j, thenreturn 1otherwise when i > j, thenreturn 0otherwise, if s[i] is same as s[j], thenreturn 2 + dp(i + 1, j - 1)otherwise, return maximum of dp(i + 1, j) and dp(i, j - 1)return ... Read More

Program to length of longest increasing path in a given matrix in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:45:43

163 Views

Suppose we have a 2D matrix, we have to find the length of the longest strictly increasing path. To traverse the path we can move up, down, left, or right nor diagonally.So, if the input is like246157339then the output will be 6, as The longest path is [1, 2, 4, 6, 7, 9]To solve this, we will follow these steps −n := row count of matrix , m := column count of matrix moves := a list of pairs to move up, down, left and right [[1, 0], [-1, 0], [0, 1], [0, -1]] Define a function dp() . This ... Read More

Program to find length of longest path with even sum in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:39:35

198 Views

Suppose we have a binary tree. we have to find the length of the longest path whose sum is an even number.So, if the input is like image, then the output will be 5, as the path is like [5, 2, 4, 8, 5], sum = 24(even).To solve this, we will follow these steps −Define a function dfs() . This will take nodeif node is null, thenreturn a pair (0, -inf)(left_0, left_1) := dfs(left of node)(right_0, right_1) := dfs(right of node)if value of node is odd, thenans := maximum of ans, (left_1 + right_0 + 1) and (left_0 + right_1 ... Read More

Program to find length of longest common subsequence of three strings in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:36:12

685 Views

Suppose we have three strings s1, s2, and s3, we have to find the length of their longest common subsequence.So, if the input is like s1 = "ababchemxde" s2 = "pyakcimde" s3 = "oauctime", then the output will be 4, as the longest common subsequence is "acme".To solve this, we will follow these steps −m := size of s1, n := size of s2, o := size of s3dp := a 3D matrix of size (o + 1) x (n + 1) x (m + 1)for i in range 1 to m, dofor j in range 1 to n, dofor ... Read More

Program to find length of longest arithmetic subsequence of a given list in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:34:52

365 Views

Suppose we have a list of numbers called nums, we have to find the length of the longest arithmetic subsequence. As we know a sequence S[i] is an arithmetic sequence when S[i+1] - S[i] have the same value for every i in range (0 ≤ i < Size of S - 1).So, if the input is like nums = [1, 4, 7, 10, 13, 20, 16], then the output will be 6, the subsequence [1, 4, 7, 10, 13, 16] is an arithmetic because the difference between each consecutive element is 3.To solve this, we will follow these steps −n := size of arrif n

Advertisements