Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:
s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.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.
Example 1:
Input: s = "3902"
Output: true
Explanation:
s = "3902"(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2s becomes "292"(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1s becomes "11""11" are the same, the output is true.Example 2:
Input: s = "34789"
Output: false
Explanation:
s = "34789".s = "7157".s = "862".s = "48".'4' != '8', the output is false.Constraints:
3 <= s.length <= 100s consists of only digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"3902"
"34789"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3461: Check If Digits Are Equal in String After Operations I
class Solution {
public boolean hasSameDigits(String s) {
char[] t = s.toCharArray();
int n = t.length;
for (int k = n - 1; k > 1; --k) {
for (int i = 0; i < k; ++i) {
t[i] = (char) ((t[i] - '0' + t[i + 1] - '0') % 10 + '0');
}
}
return t[0] == t[1];
}
}
// Accepted solution for LeetCode #3461: Check If Digits Are Equal in String After Operations I
func hasSameDigits(s string) bool {
t := []byte(s)
n := len(t)
for k := n - 1; k > 1; k-- {
for i := 0; i < k; i++ {
t[i] = (t[i]-'0'+t[i+1]-'0')%10 + '0'
}
}
return t[0] == t[1]
}
# Accepted solution for LeetCode #3461: Check If Digits Are Equal in String After Operations I
class Solution:
def hasSameDigits(self, s: str) -> bool:
t = list(map(int, s))
n = len(t)
for k in range(n - 1, 1, -1):
for i in range(k):
t[i] = (t[i] + t[i + 1]) % 10
return t[0] == t[1]
// Accepted solution for LeetCode #3461: Check If Digits Are Equal in String After Operations I
fn has_same_digits(s: String) -> bool {
let mut bs: Vec<_> = s.bytes().collect();
while bs.len() > 2 {
let mut tmp = vec![];
for i in 1..bs.len() {
let v = (bs[i - 1] + bs[i]) % 10u8;
tmp.push(v);
}
bs = tmp;
}
bs[0] == bs[1]
}
fn main() {
let ret = has_same_digits("34789".to_string());
println!("ret={ret}");
}
#[test]
fn test() {
assert!(has_same_digits("3902".to_string()));
assert!(!has_same_digits("34789".to_string()));
}
// Accepted solution for LeetCode #3461: Check If Digits Are Equal in String After Operations I
function hasSameDigits(s: string): boolean {
const t = s.split('').map(Number);
const n = t.length;
for (let k = n - 1; k > 1; --k) {
for (let i = 0; i < k; ++i) {
t[i] = (t[i] + t[i + 1]) % 10;
}
}
return t[0] === t[1];
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.