Longest Substring with at Least K Repeating Characters in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:18:21

296 Views

Suppose we have a string s, and we have to find the length of the longest substring T of that given string (consists of lowercase letters only) such that every character in T appears no less than k times. So if the string is “ababbc” and k = 2, then the output will be 3 and longest substring will be “ababb”, as there are two a’s and three b’s.To solve this, we will follow these steps −create one recursive function called longestSubstring(), this takes string s and size kif k = 1, then return the size of the stringif size ... Read More

Decode String in C++

Arnab Chakraborty
Updated on 28-Apr-2020 12:10:27

3K+ Views

Suppose we have an encoded string; we have to return its decoded string. The rule for encoding is: k[encoded_string], this indicates where the encoded_string inside the square brackets is being repeated exactly k times. We can assume that the original data does not contain any numeric characters and that digits are only for those repeat numbers, k. So if the input is like “1[ba]2[na]”, then the output will be “banana”.To solve this, we will follow these steps −create one empty stack, set i := 0while i < size of a stringif s[i] is ‘]’res := delete element from the stack ... Read More

Combination Sum IV in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:53:24

273 Views

Suppose we have an integer array with all positive numbers and all elements are unique, find the number of possible combinations, so that if we add up, we will get positive integer target.So if the array is [1, 2, 3] and the target is 4, then the possible combinations will be [[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [1, 3], [3, 1], [2, 2]], so output will be 7.To solve this, we will follow these steps −Suppose we have one recursive function called solve(), this is taking array, target and another array for dynamic ... Read More

Coin Change Problem in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:50:21

5K+ Views

Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1, 2, 5], and the amount is 11, the output is 3. This is formed using 5 + 5 + 1 = 11.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then ... Read More

Sum Root to Leaf Numbers in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:45:08

445 Views

Suppose we have a binary tree containing digits from 0-9 only, here all root-to-leaf path could represent a number.So if the tree is like −This is representing two paths 21 and 23, so the output will be 21 + 23 = 44.To solve this, we will follow these steps −Create one recursive function called dfs(), this will take root, and num. initially num = 0if the node is not nullnum := num * 10 + value of nodeif node right is not null and node left is not null, then’sum := sum + numnum := num / 10return from the ... Read More

Importance of jdeps Tool in Java 9

raja
Updated on 28-Apr-2020 11:33:16

488 Views

The jdeps is a Java Class Dependency Analyzer tool, which is a command-line tool to show the package-level or class-level dependencies of given Java class files. The input classes can be given as a path-name to a .class file, a directory, a jar file, or it will be a fully qualified class name to analyze all class files."jdeps" has included in the jdk installation since jdk 8 and, it is represented by the "%java_home%\bin\jdeps.exe" program file. If we have "%java_home%\bin" directory included in the "path" environment variable, we will run "jdeps --help" command to see a complete list of all options. Below, we can ... Read More

Unique Binary Search Trees II in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:21:10

418 Views

Suppose we have an integer n, we have to generate all structurally unique binary search trees that store values from 1 to n. So if the input is 3, then the trees will be −To solve this, we will follow these steps −Define one recursive function called generate(), this will take low and highdefine one tree node called temp.if low > high, then insert null into the temp, and return tempfor i in range low to highleft_subtree := generate(low, i – 1)right_subtree := generate(i + 1, high)current := ifor j in range 0 to size of the left_subtreefor k in ... Read More

Missing Number in Arithmetic Progression Using C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:13:44

334 Views

Suppose we have an array that represents elements of arithmetic progression in order. One element is missing. We have to find the missing element. So if arr = [2, 4, 8, 10, 12, 14], output is 6, as 6 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the difference between the middle and next to the middle is the same as diff or not. If not, then the missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the ... Read More

N-th Tribonacci Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:07:03

403 Views

Suppose we have a value n, we have to generate n-th Tribonacci number. The Tribonacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding three previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few numbers to start, are {0, 1, 1}We can solve them by following this algorithm −Algorithm• first := 0, second := 1, third := 1 • for i in range n – 3, do    o next := first ... Read More

Hamming Distance in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:05:39

4K+ Views

Consider we have two integers. We have to find the Hamming distance of them. The hamming distance is the number of bit different bit count between two numbers. So if the numbers are 7 and 15, they are 0111 and 1111 in binary, here the MSb is different, so the Hamming distance is 1.To solve this, we will follow these steps −For i = 31 down to 0b1 = right shift of x (i AND 1 time)b2 = right shift of y (i AND 1 time)if b1 = b2, then answer := answer + 0, otherwise answer := answer + ... Read More

Advertisements