@@ -86,32 +86,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Ch
86
86
87
87
<!-- solution:start -->
88
88
89
- ### Solution 1
89
+ ### Solution 1: Simulation
90
+
91
+ We can simulate the operations described in the problem until the string $s$ contains exactly two digits, and then check if these two digits are the same.
92
+
93
+ The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
90
94
91
95
<!-- tabs:start -->
92
96
93
97
#### Python3
94
98
95
99
``` python
96
-
100
+ class Solution :
101
+ def hasSameDigits (self , s : str ) -> bool :
102
+ t = list (map (int , s))
103
+ n = len (t)
104
+ for k in range (n - 1 , 1 , - 1 ):
105
+ for i in range (k):
106
+ t[i] = (t[i] + t[i + 1 ]) % 10
107
+ return t[0 ] == t[1 ]
97
108
```
98
109
99
110
#### Java
100
111
101
112
``` java
102
-
113
+ class Solution {
114
+ public boolean hasSameDigits (String s ) {
115
+ char [] t = s. toCharArray();
116
+ int n = t. length;
117
+ for (int k = n - 1 ; k > 1 ; -- k) {
118
+ for (int i = 0 ; i < k; ++ i) {
119
+ t[i] = (char ) ((t[i] - ' 0' + t[i + 1 ] - ' 0' ) % 10 + ' 0' );
120
+ }
121
+ }
122
+ return t[0 ] == t[1 ];
123
+ }
124
+ }
103
125
```
104
126
105
127
#### C++
106
128
107
129
``` cpp
108
-
130
+ class Solution {
131
+ public:
132
+ bool hasSameDigits(string s) {
133
+ int n = s.size();
134
+ string t = s;
135
+ for (int k = n - 1; k > 1; --k) {
136
+ for (int i = 0; i < k; ++i) {
137
+ t[ i] = (t[ i] - '0' + t[ i + 1] - '0') % 10 + '0';
138
+ }
139
+ }
140
+ return t[ 0] == t[ 1] ;
141
+ }
142
+ };
109
143
```
110
144
111
145
#### Go
112
146
113
147
```go
148
+ func hasSameDigits(s string) bool {
149
+ t := []byte(s)
150
+ n := len(t)
151
+ for k := n - 1; k > 1; k-- {
152
+ for i := 0; i < k; i++ {
153
+ t[i] = (t[i]-'0'+t[i+1]-'0')%10 + '0'
154
+ }
155
+ }
156
+ return t[0] == t[1]
157
+ }
158
+ ```
114
159
160
+ #### TypeScript
161
+
162
+ ``` ts
163
+ function hasSameDigits(s : string ): boolean {
164
+ const t = s .split (' ' ).map (Number );
165
+ const n = t .length ;
166
+ for (let k = n - 1 ; k > 1 ; -- k ) {
167
+ for (let i = 0 ; i < k ; ++ i ) {
168
+ t [i ] = (t [i ] + t [i + 1 ]) % 10 ;
169
+ }
170
+ }
171
+ return t [0 ] === t [1 ];
172
+ }
115
173
```
116
174
117
175
<!-- tabs: end -->
0 commit comments