Ctrl K

Fold Digits Down to a Pair

Hard
You are given a string digits. It is folded in rounds until exactly two digits remain. In one round every pair of neighbouring digits is replaced by a single digit, the last digit of their sum, (a + b) mod 10; the new digits keep their order, so the string gets one shorter. Return True if the two remaining digits are equal, and False otherwise. The string can be very long, so simulating every round will be too slow.
Example 1
digits="505" → True

One round: (5 + 0) mod 10 = 5 and (0 + 5) mod 10 = 5. What is left is 55, two equal digits.

Example 2
digits="1234" → False

1234 → 357 → 82. The digits 8 and 2 differ.

Constraints
  • 3 ≤ len(digits) ≤ 10⁵
  • digits consists of digits only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTop of a Digit Pyramid EasyDivisor Chains HardHeaviest Picks of Distinct Letters HardBalanced Stretches of a String HardK-th Postage With One Stamp Type Hard
loading Python…
Input
digits="505"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.