Skip to content

Commit 84ab62b

Browse files
committed
feat: add solutions to lc problem: No.0389
No.0389.Find the Difference
1 parent c9c8204 commit 84ab62b

File tree

5 files changed

+191
-33
lines changed

5 files changed

+191
-33
lines changed

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

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
计数器实现。
46+
**方法一:计数**
47+
48+
使用数组(`cnt`)统计 `s``t` 当中字符出现的次数:`s[i]` 进行 `cnt[s[i] - 'a']++``t[i]` 进行 `cnt[t[i] - 'a']--`
49+
50+
完成统计后,找到符合 `cnt[i] == -1``i`,返回即可(`return 'a' + i`)。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
53+
54+
**方法二:求和**
55+
56+
由于 `s``t` 只存在一个不同元素,可以统计两者所有字符 ASCII 码之和,再进行相减(`sum(t) - sum(s)`),即可得到 `t` 中那一个额外字符的 ASCII 码。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
4759

4860
<!-- tabs:start -->
4961

@@ -86,6 +98,32 @@ class Solution {
8698
}
8799
```
88100

101+
### **TypeScript**
102+
103+
```ts
104+
function findTheDifference(s: string, t: string): string {
105+
const n = s.length;
106+
const count = new Array(26).fill(0);
107+
for (let i = 0; i < n; i++) {
108+
count[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
109+
count[t.charCodeAt(i) - 'a'.charCodeAt(0)]--;
110+
}
111+
count[t.charCodeAt(n) - 'a'.charCodeAt(0)]--;
112+
return String.fromCharCode(
113+
'a'.charCodeAt(0) + count.findIndex(v => v !== 0),
114+
);
115+
}
116+
```
117+
118+
```ts
119+
function findTheDifference(s: string, t: string): string {
120+
return String.fromCharCode(
121+
[...t].reduce((r, v) => r + v.charCodeAt(0), 0) -
122+
[...s].reduce((r, v) => r + v.charCodeAt(0), 0),
123+
);
124+
}
125+
```
126+
89127
### **Rust**
90128

91129
```rust
@@ -96,18 +134,61 @@ impl Solution {
96134
let n = s.len();
97135
let mut count = [0; 26];
98136
for i in 0..n {
99-
count[(s[i] - b'a') as usize] -= 1;
100-
count[(t[i] - b'a') as usize] += 1;
137+
count[(s[i] - b'a') as usize] += 1;
138+
count[(t[i] - b'a') as usize] -= 1;
101139
}
102-
let mut res = *t.last().unwrap();
103-
for i in 0..26 {
104-
if count[i] == 1 {
105-
res = (i as u8) + b'a';
106-
break;
107-
}
140+
count[(t[n] - b'a') as usize] -= 1;
141+
char::from(b'a' + count.iter().position(|&v| v != 0).unwrap() as u8)
142+
}
143+
}
144+
```
145+
146+
```rust
147+
impl Solution {
148+
pub fn find_the_difference(s: String, t: String) -> char {
149+
let mut ans = 0;
150+
for c in s.as_bytes() {
151+
ans ^= c;
108152
}
109-
char::from(res)
153+
for c in t.as_bytes() {
154+
ans ^= c;
155+
}
156+
char::from(ans)
157+
}
158+
}
159+
```
160+
161+
### **C**
162+
163+
```c
164+
char findTheDifference(char *s, char *t) {
165+
int n = strlen(s);
166+
int count[26] = {0};
167+
for (int i = 0; i < n; i++) {
168+
count[s[i] - 'a']++;
169+
count[t[i] - 'a']--;
170+
}
171+
count[t[n] - 'a']--;
172+
int i;
173+
for (i = 0; i < 26; i++) {
174+
if (count[i]) {
175+
break;
176+
}
177+
}
178+
return 'a' + i;
179+
}
180+
```
181+
182+
```c
183+
char findTheDifference(char *s, char *t) {
184+
int n = strlen(s);
185+
char ans = 0;
186+
for (int i = 0; i < n; i++) {
187+
ans ^= s[i];
188+
ans ^= t[i];
110189
}
190+
ans ^= t[n];
191+
return ans;
111192
}
112193
```
113194

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

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ class Solution {
7474
}
7575
```
7676

77+
### **TypeScript**
78+
79+
```ts
80+
function findTheDifference(s: string, t: string): string {
81+
const n = s.length;
82+
const count = new Array(26).fill(0);
83+
for (let i = 0; i < n; i++) {
84+
count[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
85+
count[t.charCodeAt(i) - 'a'.charCodeAt(0)]--;
86+
}
87+
count[t.charCodeAt(n) - 'a'.charCodeAt(0)]--;
88+
return String.fromCharCode(
89+
'a'.charCodeAt(0) + count.findIndex(v => v !== 0),
90+
);
91+
}
92+
```
93+
94+
```ts
95+
function findTheDifference(s: string, t: string): string {
96+
return String.fromCharCode(
97+
[...t].reduce((r, v) => r + v.charCodeAt(0), 0) -
98+
[...s].reduce((r, v) => r + v.charCodeAt(0), 0),
99+
);
100+
}
101+
```
102+
77103
### **Rust**
78104

79105
```rust
@@ -84,18 +110,61 @@ impl Solution {
84110
let n = s.len();
85111
let mut count = [0; 26];
86112
for i in 0..n {
87-
count[(s[i] - b'a') as usize] -= 1;
88-
count[(t[i] - b'a') as usize] += 1;
113+
count[(s[i] - b'a') as usize] += 1;
114+
count[(t[i] - b'a') as usize] -= 1;
89115
}
90-
let mut res = *t.last().unwrap();
91-
for i in 0..26 {
92-
if count[i] == 1 {
93-
res = (i as u8) + b'a';
94-
break;
95-
}
116+
count[(t[n] - b'a') as usize] -= 1;
117+
char::from(b'a' + count.iter().position(|&v| v != 0).unwrap() as u8)
118+
}
119+
}
120+
```
121+
122+
```rust
123+
impl Solution {
124+
pub fn find_the_difference(s: String, t: String) -> char {
125+
let mut ans = 0;
126+
for c in s.as_bytes() {
127+
ans ^= c;
128+
}
129+
for c in t.as_bytes() {
130+
ans ^= c;
96131
}
97-
char::from(res)
132+
char::from(ans)
133+
}
134+
}
135+
```
136+
137+
### **C**
138+
139+
```c
140+
char findTheDifference(char *s, char *t) {
141+
int n = strlen(s);
142+
int count[26] = {0};
143+
for (int i = 0; i < n; i++) {
144+
count[s[i] - 'a']++;
145+
count[t[i] - 'a']--;
146+
}
147+
count[t[n] - 'a']--;
148+
int i;
149+
for (i = 0; i < 26; i++) {
150+
if (count[i]) {
151+
break;
152+
}
153+
}
154+
return 'a' + i;
155+
}
156+
```
157+
158+
```c
159+
char findTheDifference(char *s, char *t) {
160+
int n = strlen(s);
161+
char ans = 0;
162+
for (int i = 0; i < n; i++) {
163+
ans ^= s[i];
164+
ans ^= t[i];
98165
}
166+
ans ^= t[n];
167+
return ans;
99168
}
100169
```
101170

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
char findTheDifference(char *s, char *t) {
2+
int n = strlen(s);
3+
char ans = 0;
4+
for (int i = 0; i < n; i++) {
5+
ans ^= s[i];
6+
ans ^= t[i];
7+
}
8+
ans ^= t[n];
9+
return ans;
10+
}
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
impl Solution {
22
pub fn find_the_difference(s: String, t: String) -> char {
3-
let s = s.as_bytes();
4-
let t = t.as_bytes();
5-
let n = s.len();
6-
let mut count = [0; 26];
7-
for i in 0..n {
8-
count[(s[i] - b'a') as usize] -= 1;
9-
count[(t[i] - b'a') as usize] += 1;
3+
let mut ans = 0;
4+
for c in s.as_bytes() {
5+
ans ^= c;
106
}
11-
let mut res = *t.last().unwrap();
12-
for i in 0..26 {
13-
if count[i] == 1 {
14-
res = (i as u8) + b'a';
15-
break;
16-
}
7+
for c in t.as_bytes() {
8+
ans ^= c;
179
}
18-
char::from(res)
10+
char::from(ans)
1911
}
2012
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function findTheDifference(s: string, t: string): string {
2+
return String.fromCharCode(
3+
[...t].reduce((r, v) => r + v.charCodeAt(0), 0) -
4+
[...s].reduce((r, v) => r + v.charCodeAt(0), 0),
5+
);
6+
}

0 commit comments

Comments
 (0)