leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 318f6f2fa77c323580e80fec30b221b1f8d253e5
parent ec44665d6c0a4bcd1402ece43e6885f6edd1cfba
Author: Jack Sangdahl <jack@pngu.org>
Date:   Wed, 22 Oct 2025 22:03:30 -0600

3461. check if digits are equal in string after operations 1

Diffstat:
Acheck_digits_eq_str_op1.c | 33+++++++++++++++++++++++++++++++++
Acheck_digits_eq_str_op1.py | 32++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/check_digits_eq_str_op1.c b/check_digits_eq_str_op1.c @@ -0,0 +1,33 @@ +// 3461. Check If Digits Are Equal in String After Operations I +#include "leetcode.h" + +/* + * you are given a string 's' consisting of digits. perform the following + * operation repeatedly until the string has exactly two digits: for each pair + * of consecutive digits in 's', starting from the first digit, calculate a new + * digit as the sum of the two digits modulo 10. replace 's' with the sequence + * of newly calcluated digits, maintaining their order in which they were + * computed. return true if the final two digits in 's' are the same; otherwise + * return false. + */ + +bool hasSameDigits(char *s) { + if (*(s + 2) == '\0') + return s[0] == s[1]; + char *tmp = s; + while (*(tmp + 1) != '\0') { + (*tmp) = (((*tmp - '0') + (*(tmp + 1) - '0')) % 10) + '0'; + tmp++; + } + *(tmp) = '\0'; + return hasSameDigits(s); +} + +int main() { + char *s1i = "3902", *s1 = strdup(s1i); + char *s2i = "34789", *s2 = strdup(s2i); + printf("%d\n", hasSameDigits(s1)); // expect: 1 + printf("%d\n", hasSameDigits(s2)); // expect: 0 + free(s1); + free(s2); +} diff --git a/check_digits_eq_str_op1.py b/check_digits_eq_str_op1.py @@ -0,0 +1,32 @@ +# 3461. Check If Digits Are Equal in String After Operations I + +""" +you are given a string 's' consisting of digits. perform the following +operation repeatedly until the string has exactly two digits: for each pair +of consecutive digits in 's', starting from the first digit, calculate a new +digit as the sum of the two digits modulo 10. replace 's' with the sequence +of newly calcluated digits, maintaining their order in which they were +computed. return true if the final two digits in 's' are the same; otherwise +return false. +""" + + +class Solution(object): + def hasSameDigits(self, s): + """ + :type s: str + :rtype: bool + """ + while len(s) != 2: + tmp = "" + for i in range(1, len(s)): + j = ((ord(s[i]) - ord("a")) + (ord(s[i - 1]) - ord("a"))) % 10 + tmp += chr(j + ord("a")) + s = tmp + return s[0] == s[1] + + +if __name__ == "__main__": + obj = Solution() + print(obj.hasSameDigits(s="3902")) + print(obj.hasSameDigits(s="34789"))