Skip to content

Commit dfd08ff

Browse files
committed
feat: add solutions to lc problem: No.0389
No.0389.Find the Difference
1 parent 2ca5f40 commit dfd08ff

File tree

5 files changed

+155
-37
lines changed

5 files changed

+155
-37
lines changed

solution/0300-0399/0389.Find the Difference/README.md

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
完成统计后,找到符合 `cnt[i] == -1``i`,返回即可(`return 'a' + i`)。
5151

52-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
52+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。本题中 $C=26$。
5353

5454
**方法二:求和**
5555

@@ -66,12 +66,19 @@
6666
```python
6767
class Solution:
6868
def findTheDifference(self, s: str, t: str) -> str:
69-
counter = Counter(s)
69+
cnt = Counter(s)
7070
for c in t:
71-
if counter[c] <= 0:
71+
cnt[c] -= 1
72+
if cnt[c] < 0:
7273
return c
73-
counter[c] -= 1
74-
return None
74+
```
75+
76+
```python
77+
class Solution:
78+
def findTheDifference(self, s: str, t: str) -> str:
79+
a = sum(ord(c) for c in s)
80+
b = sum(ord(c) for c in t)
81+
return chr(b - a)
7582
```
7683

7784
### **Java**
@@ -81,23 +88,60 @@ class Solution:
8188
```java
8289
class Solution {
8390
public char findTheDifference(String s, String t) {
84-
int[] counter = new int[26];
91+
int[] cnt = new int[26];
8592
for (int i = 0; i < s.length(); ++i) {
86-
int index = s.charAt(i) - 'a';
87-
++counter[index];
93+
++cnt[s.charAt(i) - 'a'];
8894
}
89-
for (int i = 0; i < t.length(); ++i) {
90-
int index = t.charAt(i) - 'a';
91-
if (counter[index] <= 0) {
95+
for (int i = 0; ; ++i) {
96+
if (--cnt[t.charAt(i) - 'a'] < 0) {
9297
return t.charAt(i);
9398
}
94-
--counter[index];
9599
}
96-
return ' ';
97100
}
98101
}
99102
```
100103

104+
```java
105+
class Solution {
106+
public char findTheDifference(String s, String t) {
107+
int ss = 0;
108+
for (int i = 0; i < t.length(); ++i) {
109+
ss += t.charAt(i);
110+
}
111+
for (int i = 0; i < s.length(); ++i) {
112+
ss -= s.charAt(i);
113+
}
114+
return (char) ss;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
char findTheDifference(string s, string t) {
125+
int cnt[26] = {0};
126+
for (char& c : s) ++cnt[c - 'a'];
127+
for (char& c : t) if (--cnt[c - 'a'] < 0) return c;
128+
return ' ';
129+
}
130+
};
131+
```
132+
133+
```cpp
134+
class Solution {
135+
public:
136+
char findTheDifference(string s, string t) {
137+
int a = 0, b = 0;
138+
for (char& c : s) a += c;
139+
for (char& c : t) b += c;
140+
return b - a;
141+
}
142+
};
143+
```
144+
101145
### **TypeScript**
102146

103147
```ts
@@ -210,6 +254,19 @@ func findTheDifference(s, t string) byte {
210254
}
211255
```
212256

257+
```go
258+
func findTheDifference(s string, t string) byte {
259+
ss := 0
260+
for _, c := range s {
261+
ss -= int(c)
262+
}
263+
for _, c := range t {
264+
ss += int(c)
265+
}
266+
return byte(ss)
267+
}
268+
```
269+
213270
### **...**
214271

215272
```

solution/0300-0399/0389.Find the Difference/README_EN.md

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,80 @@
4444
```python
4545
class Solution:
4646
def findTheDifference(self, s: str, t: str) -> str:
47-
counter = Counter(s)
47+
cnt = Counter(s)
4848
for c in t:
49-
if counter[c] <= 0:
49+
cnt[c] -= 1
50+
if cnt[c] < 0:
5051
return c
51-
counter[c] -= 1
52-
return None
52+
```
53+
54+
```python
55+
class Solution:
56+
def findTheDifference(self, s: str, t: str) -> str:
57+
a = sum(ord(c) for c in s)
58+
b = sum(ord(c) for c in t)
59+
return chr(b - a)
5360
```
5461

5562
### **Java**
5663

5764
```java
5865
class Solution {
5966
public char findTheDifference(String s, String t) {
60-
int[] counter = new int[26];
67+
int[] cnt = new int[26];
6168
for (int i = 0; i < s.length(); ++i) {
62-
int index = s.charAt(i) - 'a';
63-
++counter[index];
69+
++cnt[s.charAt(i) - 'a'];
6470
}
65-
for (int i = 0; i < t.length(); ++i) {
66-
int index = t.charAt(i) - 'a';
67-
if (counter[index] <= 0) {
71+
for (int i = 0; ; ++i) {
72+
if (--cnt[t.charAt(i) - 'a'] < 0) {
6873
return t.charAt(i);
6974
}
70-
--counter[index];
7175
}
72-
return ' ';
7376
}
7477
}
7578
```
7679

80+
```java
81+
class Solution {
82+
public char findTheDifference(String s, String t) {
83+
int ss = 0;
84+
for (int i = 0; i < t.length(); ++i) {
85+
ss += t.charAt(i);
86+
}
87+
for (int i = 0; i < s.length(); ++i) {
88+
ss -= s.charAt(i);
89+
}
90+
return (char) ss;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
char findTheDifference(string s, string t) {
101+
int cnt[26] = {0};
102+
for (char& c : s) ++cnt[c - 'a'];
103+
for (char& c : t) if (--cnt[c - 'a'] < 0) return c;
104+
return ' ';
105+
}
106+
};
107+
```
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
char findTheDifference(string s, string t) {
113+
int a = 0, b = 0;
114+
for (char& c : s) a += c;
115+
for (char& c : t) b += c;
116+
return b - a;
117+
}
118+
};
119+
```
120+
77121
### **TypeScript**
78122

79123
```ts
@@ -186,6 +230,19 @@ func findTheDifference(s, t string) byte {
186230
}
187231
```
188232

233+
```go
234+
func findTheDifference(s string, t string) byte {
235+
ss := 0
236+
for _, c := range s {
237+
ss -= int(c)
238+
}
239+
for _, c := range t {
240+
ss += int(c)
241+
}
242+
return byte(ss)
243+
}
244+
```
245+
189246
### **...**
190247

191248
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
char findTheDifference(string s, string t) {
4+
int cnt[26] = {0};
5+
for (char& c : s) ++cnt[c - 'a'];
6+
for (char& c : t) if (--cnt[c - 'a'] < 0) return c;
7+
return ' ';
8+
}
9+
};
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
class Solution {
22
public char findTheDifference(String s, String t) {
3-
int[] counter = new int[26];
3+
int[] cnt = new int[26];
44
for (int i = 0; i < s.length(); ++i) {
5-
int index = s.charAt(i) - 'a';
6-
++counter[index];
5+
++cnt[s.charAt(i) - 'a'];
76
}
8-
for (int i = 0; i < t.length(); ++i) {
9-
int index = t.charAt(i) - 'a';
10-
if (counter[index] <= 0) {
7+
for (int i = 0; ; ++i) {
8+
if (--cnt[t.charAt(i) - 'a'] < 0) {
119
return t.charAt(i);
1210
}
13-
--counter[index];
1411
}
15-
return ' ';
1612
}
1713
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def findTheDifference(self, s: str, t: str) -> str:
3-
counter = Counter(s)
3+
cnt = Counter(s)
44
for c in t:
5-
if counter[c] <= 0:
5+
cnt[c] -= 1
6+
if cnt[c] < 0:
67
return c
7-
counter[c] -= 1
8-
return None

0 commit comments

Comments
 (0)