Find Maximum Number of Balanced Groups of Parentheses in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:16:27

471 Views

Suppose we have a string s that contains balanced parentheses "(" and ")", we have to split them into the maximum number of balanced groups.So, if the input is like "(()())()(())", then the output will be ['(()())', '()', '(())']To solve this, we will follow these steps −temp := blank stringgroups := a new listcount := 0for each character b in s, doif count is same as 0 and size of temp > 0, theninsert temp at the end of groupstemp := blank stringtemp := temp concatenate bif b is same as '(', thencount := count + 1otherwise, count := count ... Read More

Count Palindromes of Size K from Given String Characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:05:34

435 Views

Suppose we have a string s that is representing alphabet characters and a number k. We have to find the number of palindromes where we can construct of length k using only letters in s. And we can use these letters more than once if we want.So, if the input is like s = "xy", k = 4, then the output will be 4 as the palindromes are [xxxx, yyyy, xyyx, yxxy].To solve this, we will follow these steps −n := quotient of k/2x := number of unique characters in sreturn x^(n + k mod 2)Let us see the following ... Read More

Count Valid Pairs with Odd Sum in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:04:00

391 Views

Suppose we have a list of positive numbers nums, we have to find the number of valid pairs of indices (i, j), where i < j, and nums[i] + nums[j] is an odd number.So, if the input is like [5, 4, 6], then the output will be 2, as two pairs are [5, 4] and [5, 6], whose sum are odd.To solve this, we will follow these steps −e := a list by taking only the even numbers in numsreturn (size of nums - size of e) * size of eLet us see the following implementation to get better understanding ... Read More

Check Old and New Version Numbering in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:02:42

201 Views

Suppose we have a strings older and another string newer. These two are representing software package versions in the format "major.minor.patch", we have to check whether the newer version is actually newer than the older one.So, if the input is like older = "7.2.2", newer = "7.3.1", then the output will be TrueTo solve this, we will follow these steps −older := a list of major, minor, patch code of oldernewer:= a list of major, minor, patch code of newerfor i in range the size of list older, do:= older[i], n := newer[i]if n > o, thenreturn Trueotherwise when n ... Read More

Find K-Length Paths on a Binary Tree in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:01:27

320 Views

Suppose we have a binary tree which contains unique values and we also have another value k, we have to find the number of k-length unique paths in the tree. The paths can go either from parent to child or from child to parent. We will consider two paths are different when some node appears in one path but not the other.So, if the input is likek = 3, then the output will be 4, as the paths are [12, 8, 3], [12, 8, 10], [8, 12, 15], [3, 8, 10].To solve this, we will follow these steps−Define a function ... Read More

Count Elements with Odd Number of Digits in a List using Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:57:12

572 Views

Suppose we have a list of positive numbers called nums, we have to find the number of elements that have odd number of digits.So, if the input is like [1, 300, 12, 10, 3, 51236, 1245], then the output will be 4To solve this, we will follow these steps −c:= 0for i in range 0 to size of nums, dos:= digit count of nums[i]if s is odd, thenc:= c+1return cLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):       c=0       for i in range(len(nums)):       ... Read More

Find the Sum of First N Odd Numbers in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:55:44

10K+ Views

Suppose we have one number n, we have to find the sum of the first n positive odd numbers.So, if the input is like 7, then the output will be 49 as [1+3+5+7+9+11+13] = 49To solve this, we will follow these steps −if n is same as 0, thenreturn 0sum := 1, count := 0, temp := 1while count < n-1, dotemp := temp + 2sum := sum + tempcount := count + 1return sumLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       if n == 0:          return 0          sum = 1          count = 0          temp = 1          while(count

Find Total Number of Strings with Unique Characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:54:13

232 Views

Suppose we have a string s of lowercase letters, we have to find the total number of substrings that contain one unique character.So, if the input is like "xxyy", then the output will be 6 as substrings are [x, x, xx, y, y, yy]To solve this, we will follow these steps −total := 0previous := blank stringfor each character c in s, doif c is not same as previous, thenprevious := ctemp := 1otherwise, temp := temp + 1total := total + tempreturn totalLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s): ... Read More

Find Number of Nodes in a Range in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:51:34

947 Views

Suppose we have a BST, and we also have left and right bounds l and r, we have to find the count of all nodes in root whose values are present in between l and r (inclusive).So, if the input is likel = 7, r = 13, then the output will be 3, as there are three nodes: 8, 10, 12.To solve this, we will follow these steps−stack := a stack and insert root at first, count := 0while stack is not empty, donode := top element of stack, and pop elementif node is not null, thenif l

Find Number of Bit 1 in Given Number in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:50:37

149 Views

Suppose we have a number n, we have to find the number of bit 1 present in the binary representation of that number.So, if the input is like 12, then the output will be 2To solve this, we will follow these steps −count := 0while n is non-zero, docount := count + (n AND 1)n := floor of (n / 2)return countLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       count = 0       while (n):          count += n & 1          n >>= 1       return count ob = Solution() print(ob.solve(12))Input12Output2

Advertisements