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.
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]- Time: O(n^2)
- Space: O(n)