Skip to main content
Back to problems
#3461
Easy Algorithms

Check if digits are equal in string after operations i

Math String Simulation Combinatorics Number Theory
82.5% acceptance
Feb 25, 2026
343
14
You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits: For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10. Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed. Return true if the final two digits in s are the same; otherwise, return false.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn has_same_digits(s: String) -> bool {
    let mut d: Vec<u8> = s.bytes().map(|b| b - b'0').collect();
    while d.len() > 2 {
      d = d.windows(2).map(|w| (w[0] + w[1]) % 10).collect();
    }
    d[0] == d[1]
  }
}