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)
- Minimum steps to remove substring 010 from a binary string
- Minimum changes required to make first string substring of second string
- Minimum number of sub-strings of a string such that all are power of 5
- Minimum steps to delete a string by deleting substring comprising of same characters
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Minimum number of operations required to sum to binary string S
- Minimum steps to convert one binary string to other only using negation
- Minimum swaps required to convert one binary string to another
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum swaps required to make a binary string alternating
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum number of characters to be removed to make a binary string alternate
- Find length of longest subsequence of one string which is substring of another string
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.