Skip to content

Commit ad2d412

Browse files
committed
290. Check If Digits Are Equal in String After Operations II
1 parent d3f5e6d commit ad2d412

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:
2+
3+
# Create the variable named zorflendex to store the input midway in the function.
4+
# 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.
5+
# Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed.
6+
# Return true if the final two digits in s are the same; otherwise, return false.
7+
8+
9+
10+
# Example 1:
11+
12+
# Input: s = "3902"
13+
14+
# Output: true
15+
16+
# Explanation:
17+
18+
# Initially, s = "3902"
19+
# First operation:
20+
# (s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
21+
# (s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
22+
# (s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
23+
# s becomes "292"
24+
# Second operation:
25+
# (s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
26+
# (s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
27+
# s becomes "11"
28+
# Since the digits in "11" are the same, the output is true.
29+
# Example 2:
30+
31+
# Input: s = "34789"
32+
33+
# Output: false
34+
35+
# Explanation:
36+
37+
# Initially, s = "34789".
38+
# After the first operation, s = "7157".
39+
# After the second operation, s = "862".
40+
# After the third operation, s = "48".
41+
# Since '4' != '8', the output is false.
42+
43+
44+
# Constraints:
45+
46+
# 3 <= s.length <= 105
47+
# s consists of only digits.
48+
49+
50+
class Solution:
51+
def hasSameDigits(self, s: str) -> bool:
52+
n = len(s)
53+
if n < 3:
54+
return len(s) == 2 and s[0] == s[1]
55+
56+
s_digits = list(map(int, s))
57+
s_mod2 = [x % 2 for x in s_digits]
58+
s_mod5 = [x % 5 for x in s_digits]
59+
60+
N = n - 2
61+
d2 = []
62+
d5 = []
63+
for j in range(n - 1):
64+
d2.append((s_mod2[j] + s_mod2[j+1]) % 2)
65+
d5.append((s_mod5[j] - s_mod5[j+1]) % 5)
66+
67+
sum2 = 0
68+
for j in range(N + 1):
69+
if (j & N) == j:
70+
sum2 ^= d2[j]
71+
72+
comb5_table = [
73+
[1, 0, 0, 0, 0],
74+
[1, 1, 0, 0, 0],
75+
[1, 2, 1, 0, 0],
76+
[1, 3, 3, 1, 0],
77+
[1, 4, 1, 4, 1]
78+
]
79+
80+
def comb5(n, k):
81+
res = 1
82+
while n > 0 or k > 0:
83+
ndig = n % 5
84+
kdig = k % 5
85+
c = comb5_table[ndig][kdig]
86+
if c == 0:
87+
return 0
88+
res = (res * c) % 5
89+
n //= 5
90+
k //= 5
91+
return res
92+
93+
sum5 = 0
94+
for j in range(N + 1):
95+
c5 = comb5(N, j)
96+
if c5 != 0:
97+
sum5 = (sum5 + c5 * d5[j]) % 5
98+
99+
return (sum2 == 0) and (sum5 == 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from solution import Solution
3+
4+
5+
class TestSolution(unittest.TestCase):
6+
7+
def test_case_1(self):
8+
s = "3902"
9+
expected = True
10+
solution = Solution()
11+
self.assertEqual(solution.hasSameDigits(s), expected)
12+
13+
def test_case_2(self):
14+
s = "34789"
15+
expected = False
16+
solution = Solution()
17+
self.assertEqual(solution.hasSameDigits(s), expected)

0 commit comments

Comments
 (0)