Check If Digits Are Equal in String After Operations I - Problem
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
swith 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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "1234"
›
Output:
false
💡 Note:
Round 1: "1234" → "357" (1+2=3, 2+3=5, 3+4=7). Round 2: "357" → "82" (3+5=8, 5+7=12→2). Final digits 8 and 2 are different, so return false.
Example 2 — Equal Result
$
Input:
s = "999"
›
Output:
true
💡 Note:
Round 1: "999" → "88" (9+9=18→8, 9+9=18→8). Final digits are both 8, so return true.
Example 3 — Two Digits
$
Input:
s = "12"
›
Output:
false
💡 Note:
String already has two digits. 1 ≠ 2, so return false.
Constraints
- 2 ≤ s.length ≤ 1000
- s consists of digits only
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Start with digit string of length n
2
Combine Process
Sum adjacent pairs mod 10, repeat until 2 digits
3
Final Check
Return true if final digits are equal
Key Takeaway
🎯 Key Insight: Each round reduces string length by exactly 1, requiring n-2 total rounds
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code