Skip to content

Commit 0675550

Browse files
authored
feat: add solutions to lc problems: No.3461,3462 (#4102)
1 parent d56714c commit 0675550

File tree

14 files changed

+569
-16
lines changed

14 files changed

+569
-16
lines changed

solution/3400-3499/3461.Check If Digits Are Equal in String After Operations I/README.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Ch
8888

8989
<!-- solution:start -->
9090

91-
### 方法一
91+
### 方法一:模拟
92+
93+
我们可以模拟题目描述的操作,直到字符串 $s$ 中只剩下两个数字为止,判断这两个数字是否相同。
94+
95+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
9296

9397
<!-- tabs:start -->
9498

9599
#### Python3
96100

97101
```python
98-
102+
class Solution:
103+
def hasSameDigits(self, s: str) -> bool:
104+
t = list(map(int, s))
105+
n = len(t)
106+
for k in range(n - 1, 1, -1):
107+
for i in range(k):
108+
t[i] = (t[i] + t[i + 1]) % 10
109+
return t[0] == t[1]
99110
```
100111

101112
#### Java
102113

103114
```java
104-
115+
class Solution {
116+
public boolean hasSameDigits(String s) {
117+
char[] t = s.toCharArray();
118+
int n = t.length;
119+
for (int k = n - 1; k > 1; --k) {
120+
for (int i = 0; i < k; ++i) {
121+
t[i] = (char) ((t[i] - '0' + t[i + 1] - '0') % 10 + '0');
122+
}
123+
}
124+
return t[0] == t[1];
125+
}
126+
}
105127
```
106128

107129
#### C++
108130

109131
```cpp
110-
132+
class Solution {
133+
public:
134+
bool hasSameDigits(string s) {
135+
int n = s.size();
136+
string t = s;
137+
for (int k = n - 1; k > 1; --k) {
138+
for (int i = 0; i < k; ++i) {
139+
t[i] = (t[i] - '0' + t[i + 1] - '0') % 10 + '0';
140+
}
141+
}
142+
return t[0] == t[1];
143+
}
144+
};
111145
```
112146
113147
#### Go
114148
115149
```go
150+
func hasSameDigits(s string) bool {
151+
t := []byte(s)
152+
n := len(t)
153+
for k := n - 1; k > 1; k-- {
154+
for i := 0; i < k; i++ {
155+
t[i] = (t[i]-'0'+t[i+1]-'0')%10 + '0'
156+
}
157+
}
158+
return t[0] == t[1]
159+
}
160+
```
116161

162+
#### TypeScript
163+
164+
```ts
165+
function hasSameDigits(s: string): boolean {
166+
const t = s.split('').map(Number);
167+
const n = t.length;
168+
for (let k = n - 1; k > 1; --k) {
169+
for (let i = 0; i < k; ++i) {
170+
t[i] = (t[i] + t[i + 1]) % 10;
171+
}
172+
}
173+
return t[0] === t[1];
174+
}
117175
```
118176

119177
<!-- tabs:end -->

solution/3400-3499/3461.Check If Digits Are Equal in String After Operations I/README_EN.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Ch
8686

8787
<!-- solution:start -->
8888

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$.
9094

9195
<!-- tabs:start -->
9296

9397
#### Python3
9498

9599
```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]
97108
```
98109

99110
#### Java
100111

101112
```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+
}
103125
```
104126

105127
#### C++
106128

107129
```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+
};
109143
```
110144
111145
#### Go
112146
113147
```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+
```
114159

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+
}
115173
```
116174

117175
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool hasSameDigits(string s) {
4+
int n = s.size();
5+
string t = s;
6+
for (int k = n - 1; k > 1; --k) {
7+
for (int i = 0; i < k; ++i) {
8+
t[i] = (t[i] - '0' + t[i + 1] - '0') % 10 + '0';
9+
}
10+
}
11+
return t[0] == t[1];
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func hasSameDigits(s string) bool {
2+
t := []byte(s)
3+
n := len(t)
4+
for k := n - 1; k > 1; k-- {
5+
for i := 0; i < k; i++ {
6+
t[i] = (t[i]-'0'+t[i+1]-'0')%10 + '0'
7+
}
8+
}
9+
return t[0] == t[1]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean hasSameDigits(String s) {
3+
char[] t = s.toCharArray();
4+
int n = t.length;
5+
for (int k = n - 1; k > 1; --k) {
6+
for (int i = 0; i < k; ++i) {
7+
t[i] = (char) ((t[i] - '0' + t[i + 1] - '0') % 10 + '0');
8+
}
9+
}
10+
return t[0] == t[1];
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def hasSameDigits(self, s: str) -> bool:
3+
t = list(map(int, s))
4+
n = len(t)
5+
for k in range(n - 1, 1, -1):
6+
for i in range(k):
7+
t[i] = (t[i] + t[i + 1]) % 10
8+
return t[0] == t[1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function hasSameDigits(s: string): boolean {
2+
const t = s.split('').map(Number);
3+
const n = t.length;
4+
for (let k = n - 1; k > 1; --k) {
5+
for (let i = 0; i < k; ++i) {
6+
t[i] = (t[i] + t[i + 1]) % 10;
7+
}
8+
}
9+
return t[0] === t[1];
10+
}

0 commit comments

Comments
 (0)