Given a binary string str. The task is to find the smallest positive integer C such that the binary string can be cut into C pieces (sub-strings) and each sub-string should be a power of 5 with no leading zeros.
Input: str = “101101101”
The string “101101101” can be cut into three binary strings “101”, “101”, “101”
each of which is a power of 5.
Input: str = “1111101”
The string “1111101” can be cut into one binary string “1111101” which is
125 in decimal and a power of 5.
Input: str = “00000”
Strings of only zeroes is equivalent to 0 which is not a power of 5.
Approach: This problem is a simple variation of the longest increasing sub-sequence.
Iterate from i = 1 and for every str[j…i] where j = 0 & j < i, we check if the number formed from str[j..i] is a power of 5 then we update the dp array with the value of the lowest possible cut size value. After confirming that the number formed from str[j..i] in decimal is a power of 5 we calculate dp[i] = min(dp[i], dp[j] + 1).
This technique is pretty similar to finding the longest increasing sub-sequence.
Below is the implementation of the above approach:
Time complexity: O(n2)
Space complexity: 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.
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Minimum changes to a string to make all substrings distinct
- Remove minimum characters from string to split it into three substrings under given constraints
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Number of substrings of a string
- Number of substrings of one string present in other
- Sum of all substrings of a string representing a number | Set 1
- Number of even substrings in a string of digits
- Number of substrings with odd decimal value in a binary string
- Number of substrings divisible by 6 in a string of integers
- Split the binary string into substrings with equal number of 0s and 1s
- Repeat substrings of the given String required number of times
- Rearrange the string to maximize the number of palindromic substrings
- Count the number of vowels occurring in all the substrings of given string
- Count number of substrings of a string consisting of same characters
- Given a binary string, count number of substrings that start and end with 1.
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Minimum number of given operations required to convert a string to 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 email@example.com. 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.