Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

The input is small enough to simulate every transformation. All adjacent-pair sums in one round must use the same old string, so construct a separate next string instead of overwriting digits while they are still needed.

Approach

While more than two digits remain, scan adjacent pairs. Convert each pair to numbers, add them, take the result modulo ten, and append that digit to a fresh character array. Join the array to form the string for the next round.

Each round reduces the length by exactly one, so the process eventually reaches two digits. Compare those final characters and return whether they are equal. Taking modulo ten produces exactly one digit per pair, including a zero when the pair sum is ten, and preserves the specified left-to-right order.

class Solution: def hasSameDigits(self, s: str) -> bool: while len(s) > 2: arr = [] for i in range(1, len(s)): curr = (int(s[i - 1]) + int(s[i])) % 10 arr.append(str(curr)) s = "".join(arr) return s[0] == s[1]

Complexity analysis

Let n = s.length. Time: O(n^2) because each round shortens the string by one. Auxiliary space: O(n) for the next-round character array/string.