dp
with size n+1
and fill it with infinity (INF
), where n
is the length of the string s
. Set dp[0]
to 0 as the base case.s
using index i
.s[i]
is '0', skip to the next iteration.num
to 0 to store the numeric value of the current substring.i
to the end using index j
.num
by shifting its bits to the left and adding the current character's value.num
exceeds a certain limit, break the loop to avoid overflow.num
is a power of 5 using a helper function isPowerOfFive
.dp[j+1]
with the minimum of its current value and dp[i]+1
.dp[n]
is still INF
. If it is, return -1, indicating that it's impossible to partition the string into beautiful substrings. Otherwise, return dp[n]
.