#1933
Easy Algorithms Check if string is decomposable into value equal substrings
String
51.0% acceptance
Mar 31, 2026
61
15
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, decompose the string into some number of consecutive value-equal substrings where exactly one substring has a length of 2 and the remaining substrings have a length of 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.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn is_decomposable(s: String) -> bool {
let bytes = s.as_bytes();
let mut i = 0;
let mut has_two = false;
while i < bytes.len() {
let ch = bytes[i];
let mut j = i;
while j < bytes.len() && bytes[j] == ch {
j += 1;
}
let len = j - i;
let rem = len % 3;
if rem == 1 {
return false;
}
if rem == 2 {
if has_two {
return false;
}
has_two = true;
}
i = j;
}
has_two
}
}