Given a string S composed of 0 and 1. Find the minimum splits such that the substring is a binary representation of the power of 4 or 6 with no leading zeros. Print -1 if no such partitioning is possible.
Input: 100110110 Output: 3 The string can be split into a minimum of three substrings 100(power of 4), 110 (power of 6) and 110(power of 6). Input : 00000 Output : -1 0 is not a power of 4 or 6.
A simple solution is to split the string recursively at different indices and check if each split is a power of 4 or 6. Start with index 0 and split str from other string. If it is a power of 4 or 6 then call recursively for index 1 and perform the same operation. When an entire string is split check if a total number of partitions are minimum so far or not. Then split str[0..1], check if it is the power of 4 or 6 and then call recursively for rest string. Compare partitions with minimum so far at the end of string traversal. This approach will be exponential in time.
An efficient solution is to use Dynamic Programming. A 1-D dp table is created in which dp[i] stores minimum number of partitions required to split string str[i..n-1] into substrings that are power of 4 or 6. Suppose we are at index i and str[i..j] is power of 4 or 6, then minimum number of partitions will be minimum number of partitions to split str[j+1..n-1] plus one partition to split str[i..j] from string, that is dp[j+1] + 1. Hence the recurrence relation for (j!=(n-1)) and (dp[j + 1]!=-1) will be:
dp[i] = min(dp[i], dp[j + 1] + 1)
Time Complexity: O(n^2*log(x)), x = largest power of 4 or 6 obtainable from input string.
Auxiliary Space: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum splits in binary string such that each substring is divisible by given odd number
- Count possible splits of sum N into K integers such that the minimum is at least P
- Maximum number of splits of a binary number
- Minimum splits required to convert a number into prime segments
- Minimize splits to generate monotonous Substrings from given String
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Minimum K such that every substring of length atleast K contains a character c
- Maximize the numbers of splits in an Array having sum divisible by 3
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum string such that every adjacent character of given string is still adjacent
- Minimum length of substring whose rotation generates a palindromic substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Minimum number of sub-strings of a string such that all are power of 5
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- String formed with middle character of every right substring followed by left sequentially
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Minimum steps to remove substring 010 from a binary string
- Minimum substring flips required to convert given binary string to another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.