Found 10784 Articles for Python

Word Break II in Python

Arnab Chakraborty
Updated on 26-May-2020 13:37:37

397 Views

Suppose we have a non-empty string s and a dictionary called wordDict, this dictionary is containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. We have to find all such possible sentences. “appleraincoat” and dictionary is [“app”, “apple”, “rain”, “coat”, “raincoat”]To solve this, we will follow these steps −Create one map memoDefine a method called solve, this will take string and wordDictif s is null, then return empty listif s in memo, then −return memo[s]create an array retfor i in range 1 to size of sif substring ... Read More

Longest Consecutive Sequence in Python

Arnab Chakraborty
Updated on 26-May-2020 13:10:25

4K+ Views

Suppose we have an array of integers. We have to find the length of the longest consecutive elements sequence. So if the input is like [100, 4, 250, 1, 3, 2], answer will be 4, as the longest consecutive sequence is [1, 2, 3, 4].To solve this, we will follow these steps −make the array set, longest := 0for i in range array −if i – 1 is not in a −current := i, streak := 0while i in a −increase i by 1, increase streak by 1longest := max of longest and streakreturn longestExampleLet us see the following implementation ... Read More

Binary Tree Maximum Path Sum in Python

Arnab Chakraborty
Updated on 26-May-2020 13:07:57

719 Views

Suppose we have one non-empty binary tree. We have to find the path sum. So here, a path is any sequence of nodes from some starting node to any node in the where the parent-child connections are present. The path must contain at least one node and does not need to go through the root node. So if the input tree is −Here the output will be 32.To solve this, we will follow these steps −Define one method called solve(), this will take nodeif node is null or the value of node is 0, then return 0left := max of ... Read More

Jump Game II in Python

Arnab Chakraborty
Updated on 26-May-2020 12:01:30

707 Views

Suppose we have one array of integers, where all elements are positive. The initial starting point is at index 1. Each element in the array represents our maximum jump length at that position. Our goal is to reach to the final cell with less number of jumps. So if the array is like [2, 3, 1, 1, 4], and then the output will be 2, as we can jump to index 1 from 0, then jump to index 4, that is the last index.To solve this, we will follow these steps −end := 0, jumps := 0, farthest := 0for ... Read More

Wildcard Matching in Python

Arnab Chakraborty
Updated on 26-May-2020 11:58:18

4K+ Views

Suppose we have an input string s and another input string p. Here is the main string and p is the pattern. We have to define one method, that can match pattern in the string. So we have to implement this for a regular expression, that supports wildcard characters like ‘?’ And ‘*’.Dot ‘?’ Matches any single characterStar ‘*’ Matches zero or more characters.So for example, if the input is like s = “aa” and p = “a?”, then it will be true, for the same input string, if the patter is “?*”, then it will be true.To solve this, ... Read More

Trapping Rain Water in Python

Arnab Chakraborty
Updated on 26-May-2020 11:55:41

779 Views

Suppose we have an array of n non-negative integers. These are representing an elevation map where the width of each bar is 1, we have to compute how much water it is able to trap after raining. So the map will be like −Here we can see there are 6 blue boxes, so the output will be 6.To solve this, we will follow these steps −Define a stack st, water := 0 and i := 0while i < size of heightif is stack is empty or height[stack top] >= height[i], then push i into stack, increase i by 1otherwisex := ... Read More

First Missing Positive in Python

Arnab Chakraborty
Updated on 26-May-2020 11:51:41

713 Views

Suppose we have one unsorted integer array; we have to find the smallest missing positive number. So if the array is like [4, -3, 1, -1], then the result will be 2.To solve this, we will follow these steps −set i := 0 and update array nums by adding one 0 before all numbersfor i in range 0 to length of numswhile nums[i] >= 0 and nums[i] < length of nums and nums[nums[i]] is not nums[i] −nums[nums[i]] := nums[i]nums[i] := nums[nums[i]]num := 1for i in range 1 to length of numsif num = nums[i], then increase num by 1return numExampleLet ... Read More

Longest Valid Parentheses in Python

Arnab Chakraborty
Updated on 26-May-2020 11:41:17

1K+ Views

Suppose we have a string, with opening and closing parentheses. We have to find the longest length of the valid (well-formed) parentheses. So if the input is like “))(())())”, then the result will be 6, as the valid string is “(())()”.To solve this, we will follow these steps −Make a stack, and insert -1., set ans := 0for i in range 0 to length of stack – 1if s[i] is opening parentheses, then insert i into stackotherwiseif stack is not empty and top of stack is not -1 and s[stack top] is opening parentheses, thentop element from stackans := max ... Read More

Merge k Sorted Lists in Python

Arnab Chakraborty
Updated on 26-May-2020 11:30:07

744 Views

Suppose we have some lists, these are sorted. We have to merge these lists into one list. To solve this, we will use the heap data structure. So if the lists are [1, 4, 5], [1, 3, 4], [2, 6], then the final list will be [1, 1, 2, 3, 4, 4, 5, 6].To solve this, we will follow these steps −make one heapfor each linked list l in lists −if is in not 0, then insert I into a heapres := null and res_next := nullDo one infinite loop −temp := min of heapif heap has no element, then ... Read More

Regular Expression Matching in Python

Arnab Chakraborty
Updated on 26-May-2020 11:20:39

387 Views

Suppose we have an input string s and another input string p. Here s is the main string and p is the pattern. We have to define one method, that can match patterns in the string. So we have to implement this for a regular expression, that supports ‘.’ And ‘*’.Dot ‘.’ Matches any single characterStar ‘*’ Matches zero or more of the preceding element.So for example, if the input is like s = “aa” and p = “a.”, then it will be true, for the same input string, if the patter is “.*”, then it will be true.To solve ... Read More

Advertisements