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, 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.

 

Example 1:

Input: s = "000111000"
Output: false
Explanation: s cannot be decomposed according to the rules because ["000", "111", "000"] does not have a substring of length 2.

Example 2:

Input: s = "00011111222"
Output: true
Explanation: s can be decomposed into ["000", "111", "11", "222"].

Example 3:

Input: s = "011100022233"
Output: false
Explanation: s cannot be decomposed according to the rules because of the first '0'.

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of only digits '0' through '9'.

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

impl Solution {
    fn is_decomposable(s: String) -> bool {
        let mut count = 0;
        let mut prev = ' ';
        let mut two = false;
        for c in s.chars() {
            dbg!(c);
            dbg!(count);
            if c == prev {
                count += 1;
            } else {
                if count % 3 == 1 {
                    return false;
                }
                if count % 3 == 2 {
                    if two {
                        return false;
                    } else {
                        two = true;
                    }
                }
                count = 1;
                prev = c;
            }
        }
        if count % 3 == 1 {
            return false;
        }
        if count % 3 == 2 {
            if two {
                return false;
            } else {
                two = true;
            }
        }
        two
    }
}

#[test]
fn test() {
    let s = "000111000".to_string();
    let res = false;
    assert_eq!(Solution::is_decomposable(s), res);
    let s = "00011111222".to_string();
    let res = true;
    assert_eq!(Solution::is_decomposable(s), res);
    let s = "011100022233".to_string();
    let res = false;
    assert_eq!(Solution::is_decomposable(s), res);
}