Segregate 1s and 0s in Separate Halves of a Binary String

Shubham Vora
Updated on 28-Jul-2023 13:10:17

322 Views

In this tutorial, we need to separate all 1s and 0s of the given binary string in the two halves. Here, we need to take a substring from the given string and reverse it to separate 0s and 1s in different parts. Ultimately, we need to count the total number of reversals required for substring to separate 1s and 0s in two halves. Problem statement − We have given a binary string of even length. We need to take any substring from the given string multiple times and reverse it to separate it into two halves. We need to print ... Read More

Modify String by Replacing All Occurrences of Given Characters

Shubham Vora
Updated on 28-Jul-2023 13:07:54

257 Views

In this problem, we need to replace the characters of the given string according to the characters given in the array of pairs of characters. We will discuss two different approaches to solving the problem. In the first approach, we iterate through the characters of the given string and pairs of characters to replace each character. In the second approach, we will use an array of length 26 to store the replaced character related to each character and change the character of the given string. Problem statement − We have given a string str containing N lowercase alphabetic characters. Also, ... Read More

Minimum Removals Required to Rearrange String into Palindrome

Shubham Vora
Updated on 28-Jul-2023 13:06:05

1K+ Views

In this problem, we need to remove the minimum characters from the string and rearrange the remaining characters to make the string palindromic. To solve the problem, the first question we should ask ourselves is when we can make string palindromic. In below two cases, we can make string palindromic. If each character’s frequency is even in the given string. If only one character’s frequency is odd and all other characters' frequency is even. So, we need to remove minimum characters to make each character’s frequency even except for any single character. Problem statement − We have given ... Read More

Minimize Removal of Substring of 0s from Circular Binary String

Shubham Vora
Updated on 28-Jul-2023 13:04:06

152 Views

In this problem, we require to remove all zeros from the given binary string. Also, we require to remove pair of consecutive zeros at once and count the total number of pairs of zeros removal. We can solve the problem by counting the number of pairs of consecutive zeros in the given string. In this tutorial, we will learn two different solutions to solve the problem. Problem statement − We have given circular binary string str of length N. We need to find the minimum number of consecutive zeros required to remove all zeros from the string. Sample Examples Input ... Read More

Minimize Flips to Avoid Consecutive 0s in String

Shubham Vora
Updated on 28-Jul-2023 12:58:09

216 Views

Here, we require to manipulate the binary string so that it doesn’t contain any consecutive zeros. If we find consecutive zeros, we need to change any zero to 1. So, we require to count the total number of 0 to 1 conversion we should make to remove all consecutive zeros from the string. Problem statement − We have given a binary string ‘str’ containing only 0 and 1. We require to find the minimum number of flips required so that the resultant string doesn’t contain any consecutive zeros. Sample Examples Input – 0101001 Output – 1 Explanation ... Read More

Longest Non-Increasing Subsequence in a Binary String

Shubham Vora
Updated on 28-Jul-2023 12:55:09

350 Views

In this problem, we require to find the longest non-increasing subsequence of the given string. The meaning of non-increasing is either character should be the same or in decreasing order. As binary string contains only ‘0’ and ‘1’, the resultant string should start with ‘1’ and end with ‘0’, or start and end with either ‘0’ or ‘1’. To solve the problem, we will count prefix ‘1’s and suffix ‘0’ at each position of the string and find the maximum sum of prefix ‘1’s and suffix ‘0’s. Problem statement − We have given binary string str. We need to ... Read More

Length of Longest Subset Consisting of 0s and 1s from Array of Strings

Shubham Vora
Updated on 28-Jul-2023 12:52:55

165 Views

In this problem, we need to find the longest subset containing at most A 0s and B1s. All we need to do is find all possible subsets using the array elements and find the longest subset containing maximum A 0s and B1. In this tutorial, first, we will learn the recursive approach to solve the problem. After that, we will optimize the code using the dynamic programming approach. Problem statement − We have given an array containing N binary strings. Also, we have given A and B integers. We need to make the longest subset using the given binary strings ... Read More

Construct K-Length Binary String from Array Based on Conditions

Shubham Vora
Updated on 28-Jul-2023 12:50:54

177 Views

In this tutorial, we require to construct a binary string of length K such that it should contain ‘1’ at the ith index if a subset-sum equal to I is possible using array elements. We will learn two approaches to solving the problem. In the first approach, we will use a dynamic programming approach to check whether the subset sum equal to index ‘I’ is possible. In the second approach, we will use a bitset to find all possible sums using array elements. Problem statement − We have given an array containing N integers. Also, we have given integer M ... Read More

Check If a String Can Be Split Into 3 Substrings

Shubham Vora
Updated on 28-Jul-2023 12:48:15

163 Views

In this problem, we need to split the given string in such a way that the third substring can be a substring of the first two substrings. Let’s think about the solution. The third string can be a substring of the first two string only if the first two string contains all characters of the third string. So, we need to find at least one character in the given string with a frequency of more than 3, and we can take the third substring of the single character. Problem statement − We have given a string str containing the N ... Read More

Check If a Binary String Can Be Sorted in Decreasing Order

Shubham Vora
Updated on 28-Jul-2023 12:46:14

194 Views

In this problem, we need to sort the given binary string in decreasing order by removing only non-adjacent elements. To solve the problem, we require to remove all zeros which are placed before ones in the binary string. If we find two consecutive ones after two consecutive zeros at any position in the string, it means we can’t sort the string in decreasing order. Otherwise, we can sort it out in each case. Problem statement − We have given binary string str with a length equal to N. We need to check whether we can sort the given string in ... Read More

Advertisements