Found 26504 Articles for Server Side Programming

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

1K+ 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

896 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

Sudoku Solver in C++

Arnab Chakraborty
Updated on 26-May-2020 11:48:01

17K+ Views

Suppose we have a Sudoku grid and we have to solve this famous number maze problem, Sudoku. We know that Sudoku is a 9 x 9 number grid, and the whole grid are also divided into 3 x 3 boxes There are some rules to solve the Sudoku.We have to use digits 1 to 9 for solving this problem.One digit cannot be repeated in one row, one column or in one 3 x 3 box.Using backtracking algorithm, we will try to solve Sudoku problem. When some cell is filled with a digit, it checks whether it is valid or not. ... 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

Substring with Concatenation of All Words in C++

Arnab Chakraborty
Updated on 26-May-2020 11:38:04

184 Views

Suppose we have a string, s, and we also have a list of words, words present in the array are all of the same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “barfoothefoobarman” and words are [“foo”, “bar”], then the output will be [0, 9]. This is because the substring starting at index 0 and 9 are “barfoo” and “foobar”.To solve this, we will follow these steps −Define a method called ok(), this will take ... Read More

Reverse Nodes in k-Group in C++

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

267 Views

Suppose we have a linked list, we have to reverse the nodes of the linked list k at a time and return its modified list. Here k is a positive integer and is less than or equal to the length of the linked list. So if the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.So if the linked list is like [1, 2, 3, 4, 5, 6, 7] and k is 3, then the result will be [3, 2, 1, 6, 5, 4, 7].To solve this, we will ... Read More

Merge k Sorted Lists in Python

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

952 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

518 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

Median of Two Sorted Arrays in C++

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

456 Views

Suppose we have two arrays; these arrays are sorted. So we have to find the median of these two arrays. So if the arrays are like [1,5,8] and [2,3,6,9], then the answer will be 5.To solve this, we will follow these steps −Define a function findMedianSortedArrays, this will take nums1 and nums2 arraysif size of nums1 > size of nums2, then,Call the function return findMedianSortedArrays(nums2, nums1)x := size of nums1, y := size of nums2low := 0, high := xtotalLength := x + ywhile low

Advertisements