-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2767-partition-string-into-minimum-beautiful-substrings.js
More file actions
52 lines (47 loc) · 1.46 KB
/
2767-partition-string-into-minimum-beautiful-substrings.js
File metadata and controls
52 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 2767. Partition String Into Minimum Beautiful Substrings
* https://leetcode.com/problems/partition-string-into-minimum-beautiful-substrings/
* Difficulty: 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:
* - It doesn't contain leading zeros.
* - It's the binary representation of a number that is a power of 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.
*/
/**
* @param {string} s
* @return {number}
*/
var minimumBeautifulSubstrings = function(s) {
const n = s.length;
const powersOfFive = new Set();
let power = 1;
while (power.toString(2).length <= n) {
powersOfFive.add(power.toString(2));
power *= 5;
}
const result = findMinPartitions(0);
return result === Infinity ? -1 : result;
function findMinPartitions(index) {
if (index === n) return 0;
if (s[index] === '0') return Infinity;
let minPartitions = Infinity;
for (let end = index + 1; end <= n; end++) {
const substring = s.slice(index, end);
if (powersOfFive.has(substring)) {
const next = findMinPartitions(end);
if (next !== Infinity) {
minPartitions = Math.min(minPartitions, 1 + next);
}
}
}
return minPartitions;
}
};