Skip to content

Commit d89a8fc

Browse files
committed
feat: leetcode 面试题 01.02
1 parent 61b3e82 commit d89a8fc

File tree

8 files changed

+112
-4
lines changed

8 files changed

+112
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:505-green" alt="进度:505">
5+
<img src="https://img.shields.io/badge/-进度:506-green" alt="进度:506">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@
361361
"title": "检测正方形",
362362
"path": "./problemset/detect-squares/README.md",
363363
"difficulty": "中等"
364+
},
365+
{
366+
"id": "面试题 01.02",
367+
"title": "判定是否互为字符重排",
368+
"path": "./problemset/check-permutation-lcci/README.md",
369+
"difficulty": "简单"
364370
}
365371
]
366372
},

assets/data/topics.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5048,5 +5048,15 @@
50485048
"difficulty": "困难",
50495049
"url": "https://leetcode.cn/problems/missing-two-lcci/",
50505050
"path": "./problemset/missing-two-lcci/README.md"
5051+
},
5052+
{
5053+
"id": "面试题 01.02",
5054+
"title": {
5055+
"cn": "判定是否互为字符重排",
5056+
"en": "check-permutation-lcci"
5057+
},
5058+
"difficulty": "简单",
5059+
"url": "https://leetcode.cn/problems/check-permutation-lcci/",
5060+
"path": "./problemset/check-permutation-lcci/README.md"
50515061
}
50525062
]

assets/docs/CATEGORIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
| 1832. [判断句子是否为全字母句](../../problemset/check-if-the-sentence-is-pangram/README.md) | 简单 |
6565
| 2006. [差的绝对值为 K 的数对数目](../../problemset/count-number-of-pairs-with-absolute-difference-k/README.md) | 简单 |
6666
| 2013. [检测正方形](../../problemset/detect-squares/README.md) | 中等 |
67+
| 面试题 01.02. [判定是否互为字符重排](../../problemset/check-permutation-lcci/README.md) | 简单 |
6768

6869
## 链表
6970

assets/docs/TOPICS.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,6 @@
998998

999999
[面试题 17.11. 单词距离](../../problemset/find-closest-lcci/README.md)
10001000

1001-
[面试题 17.19. 消失的两个数字](../../problemset/missing-two-lcci/README.md)
1002-
10031001
[剑指 Offer II 114. 外星文字典](../../problemset/alien-dictionary/README.md)
10041002

10051003
[剑指 Offer II 029. 排序的循环链表](../../problemset/sorted-circular-linked-list/README.md)
@@ -1008,4 +1006,8 @@
10081006

10091007
[剑指 Offer II 041. 滑动窗口的平均值](../../problemset/average-value-of-sliding-window/README.md)
10101008

1011-
[剑指 Offer II 115. 重建序列](../../problemset/rebuild-sequence/README.md)
1009+
[剑指 Offer II 115. 重建序列](../../problemset/rebuild-sequence/README.md)
1010+
1011+
[面试题 17.19. 消失的两个数字](../../problemset/missing-two-lcci/README.md)
1012+
1013+
[面试题 01.02. 判定是否互为字符重排](../../problemset/check-permutation-lcci/README.md)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 判定是否互为字符重排
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/check-permutation-lcci/
6+
7+
## 题目
8+
9+
给定两个字符串 `s1``s2`,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
10+
11+
### 示例
12+
13+
#### 示例 1:
14+
15+
```
16+
输入: s1 = "abc", s2 = "bca"
17+
输出: true
18+
```
19+
20+
#### 示例 2:
21+
22+
```
23+
输入: s1 = "abc", s2 = "bad"
24+
输出: false
25+
```
26+
27+
## 解题
28+
29+
```ts
30+
/**
31+
* 时间复杂度 O(N) 空间复杂度 O(N)
32+
* @param s1
33+
* @param s2
34+
* @returns
35+
*/
36+
export function CheckPermutation(s1: string, s2: string): boolean {
37+
const map = new Map<string, number>()
38+
for (const s of s1)
39+
map.set(s, (map.get(s) || 0) + 1)
40+
41+
for (const s of s2) {
42+
const count = map.get(s)!
43+
if (!count) return false
44+
if (count === 1) map.delete(s)
45+
else map.set(s, count - 1)
46+
}
47+
48+
return map.size === 0
49+
}
50+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { CheckPermutation } from '.'
3+
4+
describe('判定是否互为字符重排', () => {
5+
testCase(CheckPermutation)
6+
})
7+
8+
function testCase(fn: (s1: string, s2: string) => boolean) {
9+
it.each([
10+
[
11+
'abc', 'bca', true,
12+
],
13+
[
14+
'abc', 'bad', false,
15+
],
16+
])('示例%#', (s1, s2, expected) => {
17+
expect(fn(s1, s2)).toBe(expected)
18+
})
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 时间复杂度 O(N) 空间复杂度 O(N)
3+
* @param s1
4+
* @param s2
5+
* @returns
6+
*/
7+
export function CheckPermutation(s1: string, s2: string): boolean {
8+
const map = new Map<string, number>()
9+
for (const s of s1)
10+
map.set(s, (map.get(s) || 0) + 1)
11+
12+
for (const s of s2) {
13+
const count = map.get(s)!
14+
if (!count) return false
15+
if (count === 1) map.delete(s)
16+
else map.set(s, count - 1)
17+
}
18+
19+
return map.size === 0
20+
}

0 commit comments

Comments
 (0)