#2767
Partition String Into Minimum Beautiful Substrings
specialist · 810 · lc medium +31 · verified · 54% accepted · 383 likes · top 46%
Description
You are given a binary string s. Divide it into one or more substrings so that every substring qualifies as beautiful.
A substring is beautiful when:
- It contains no leading zeros.
- Its binary value equals a power of 5.
Return the fewest number of substrings in any valid partition, or -1 if no valid partition is possible.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "1011"
Output: 2
Explanation: We can paritition the given string into ["101", "1"].
- The string "101" does not contain leading zeros and is the binary representation of integer 51 = 5.
- The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1.
It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into.
Example 2:
Input: s = "111"
Output: 3
Explanation: We can paritition the given string into ["1", "1", "1"].
- The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1.
It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into.
Example 3:
Input: s = "0"
Output: -1
Explanation: We can not partition the given string into beautiful substrings.
Code
1
2
3