Medium
Given a binary string s
, partition the string into one or more substrings such that each substring is beautiful.
A string is beautiful if:
5
.Return the minimum number of substrings in such partition. If it is impossible to partition the string s
into beautiful substrings, return -1
.
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”].
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”].
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.
Constraints:
1 <= s.length <= 15
s[i]
is either '0'
or '1'
.class Solution {
fun minimumBeautifulSubstrings(s: String): Int {
val set: MutableSet<String> = HashSet()
set.add("1")
set.add("101")
set.add("11001")
set.add("1111101")
set.add("1001110001")
set.add("110000110101")
set.add("11110100001001")
val result = minimumBeautifulSubstringsHelper(s, 0, set, 0)
return if (result == Int.MAX_VALUE) {
-1
} else {
result
}
}
private fun minimumBeautifulSubstringsHelper(s: String, index: Int, set: Set<String>, count: Int): Int {
if (index >= s.length) {
return count
}
var minResult = Int.MAX_VALUE
for (i in index..s.length) {
if (set.contains(s.substring(index, i))) {
val result = minimumBeautifulSubstringsHelper(s, i, set, count + 1)
minResult = minResult.coerceAtMost(result)
}
}
return minResult
}
}