[LeetCode] Check if String Is Decomposable Into Value-Equal Substrings

1933. Check if String Is Decomposable Into Value-Equal Substrings

A value-equal string is a string where all characters are the same.

  • For example, "1111" and "33" are value-equal strings.
  • In contrast, "123" is not a value-equal string.

Given a digit string s``2``3.

Return true if you can decompose s according to the above rules. Otherwise, return false.

A substring is a contiguous sequence of characters in a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
bool isDecomposable(string s) {
bool fl = false;
while(s.size()) {
char ch = s.back();
int cnt = 0;
while(s.size() and s.back() == ch) s.pop_back(), cnt++;
if(cnt % 3 == 0) continue;
if((cnt - 2) % 3 or fl) return false;
fl = true;
}
return fl;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/14/PS/LeetCode/check-if-string-is-decomposable-into-value-equal-substrings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.