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)
- Minimum changes to a string to make all substrings distinct
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Number of substrings of a string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- 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 divisible by 6 in a string of integers
- Number of substrings with odd decimal value in a binary string
- Count the number of vowels occurring in all the substrings of given string
- Rearrange the string to maximize the number of palindromic substrings
- 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
- Replace two substrings (of a string) with each other
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.
Improved By : AnkitRai01