Skip to content

Commit ada459e

Browse files
committed
https://leetcode.cn/problems/buddy-strings
1 parent 392389f commit ada459e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/buddy-strings
49+
4850
https://leetcode.cn/problems/maximum-frequency-stack
4951

5052
https://leetcode.cn/problems/calculate-amount-paid-in-taxes

buddy-strings/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function buddyStrings(A: string, B: string): boolean {
2+
if (A.length !== B.length || A.length + B.length <= 2) return false;
3+
if (A === B) {
4+
return A.length > new Set(A).size;
5+
}
6+
const indexArr: number[] = [];
7+
for (let index = 0; index < A.length; index++) {
8+
if (A[index] !== B[index]) {
9+
indexArr.push(index);
10+
}
11+
}
12+
if (
13+
indexArr.length === 2 &&
14+
A[indexArr[0]] === B[indexArr[1]] &&
15+
B[indexArr[0]] === A[indexArr[1]]
16+
) {
17+
return true;
18+
}
19+
return false;
20+
}
21+
export default buddyStrings;

0 commit comments

Comments
 (0)