leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2767.cpp (605B)
0 class Solution { 1 int rec(const char *s, const int n, int i) const { 2 if (i == n) return 0; 3 if (s[i] == '0') return -1; 4 5 int res = INT_MAX, crnt = 0; 6 for (; i < n; i++) { 7 crnt = (crnt << 1) | (s[i] & 1); 8 if (15625 % crnt == 0) { 9 const int next = rec(s, n, i + 1); 10 if (next == -1) continue; 11 res = min(res, 1 + next); 12 } 13 } 14 15 return res != INT_MAX ? res : -1; 16 } 17 18 public: 19 int minimumBeautifulSubstrings(const string &s) const { return rec(s.data(), size(s), 0); } 20 };