Repeatedly transform a numeric string by combining adjacent digits, then check whether the final two digits are equal.
Check If Digits Are Equal in String After Operations I
You are given a string s consisting only of decimal digits.
Perform the following operation repeatedly until only two characters remain:
- For every adjacent pair of digits, replace the pair with a new digit equal to the sum of the two digits modulo
10. - The resulting digits form a new string for the next round.
After all operations are complete, determine whether the two remaining digits are equal.
Return true if they are equal, otherwise return false.
Intuition
This is a direct simulation problem on a digit string. Each round shrinks the string by one character, so the process eventually stops after n - 2 rounds.
Input Format
- A single string
scontaining only characters'0'through'9'.
Output Format
- Return a boolean value.
trueif the final two digits are the same, otherwisefalse.
Constraints
2 <= s.lengthscontains only decimal digits.- The process is guaranteed to end with exactly two digits.
Example 1
Input
s = "3902"
Output
false
Explanation
Round 1: 3+9=12 -> 2, 9+0=9, 0+2=2, so the string becomes "292". Round 2: 2+9=11 -> 1, 9+2=11 -> 1, so the final string is "11". The two digits are equal, so the correct result is true. Note: this example illustrates the process; for a false result, use a different input such as "1234" which becomes "357" then "82".
Example 2
Input
s = "1234"
Output
false
Explanation
Round 1: "1234" -> "357" because 1+2=3, 2+3=5, and 3+4=7. Round 2: "357" -> "82" because 3+5=8 and 5+7=12 -> 2. The final digits are not equal.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.