Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created October 23, 2025 21:33
Show Gist options
  • Select an option

  • Save Ifihan/09c0d1d9d2fa90f3c0d019747de4c4c1 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/09c0d1d9d2fa90f3c0d019747de4c4c1 to your computer and use it in GitHub Desktop.
Check If Digits Are Equal in String After Operations I

Question

Approach

To solve this, I repeatedly reduced the string by replacing it with the sequence formed by taking the sum of each pair of adjacent digits modulo 10. I continued this process until only two digits remained. Then, I simply checked if those final two digits were equal—returning True if they were and False otherwise.

Implementation

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

Complexities

  • Time: O(n^2)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment