-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55c359d
commit ece12e0
Showing
3 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
### 题目描述 | ||
|
||
这是 LeetCode 上的 **[面试题 01.02. 判定是否互为字符重排](https://leetcode.cn/problems/check-permutation-lcci/solution/by-ac_oier-qj3j/)** ,难度为 **简单**。 | ||
|
||
Tag : 「模拟」 | ||
|
||
|
||
|
||
给定两个字符串 `s1` 和 `s2`,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。 | ||
|
||
示例 1: | ||
``` | ||
输入: s1 = "abc", s2 = "bca" | ||
输出: true | ||
``` | ||
示例 2: | ||
``` | ||
输入: s1 = "abc", s2 = "bad" | ||
输出: false | ||
``` | ||
|
||
说明: | ||
* $0 <= len(s1) <= 100$ | ||
* $0 <= len(s2) <= 100$ | ||
|
||
--- | ||
|
||
### 模拟 | ||
|
||
根据题意,对两字符串进行词频统计,统计过程中使用变量 `tot` 记录词频不同的字符个数。 | ||
|
||
Java 代码: | ||
```Java | ||
class Solution { | ||
public boolean CheckPermutation(String s1, String s2) { | ||
int n = s1.length(), m = s2.length(), tot = 0; | ||
if (n != m) return false; | ||
int[] cnts = new int[128]; | ||
for (int i = 0; i < n; i++) { | ||
if (++cnts[s1.charAt(i)] == 1) tot++; | ||
if (--cnts[s2.charAt(i)] == 0) tot--; | ||
} | ||
return tot == 0; | ||
} | ||
} | ||
``` | ||
TypeScript 代码: | ||
```TypeScript | ||
function CheckPermutation(s1: string, s2: string): boolean { | ||
let n = s1.length, m = s2.length, tot = 0 | ||
if (n != m) return false | ||
const cnts = new Array<number>(128).fill(0) | ||
for (let i = 0; i < n; i++) { | ||
if (++cnts[s1.charCodeAt(i)] == 1) tot++ | ||
if (--cnts[s2.charCodeAt(i)] == 0) tot-- | ||
} | ||
return tot == 0 | ||
}; | ||
``` | ||
Python3 代码: | ||
```Python3 | ||
class Solution: | ||
def CheckPermutation(self, s1: str, s2: str) -> bool: | ||
return Counter(s1) == Counter(s2) | ||
``` | ||
* 时间复杂度:$O(n)$ | ||
* 空间复杂度:$O(C)$,其中 $C = 128$ 为字符集大小 | ||
|
||
--- | ||
|
||
### 最后 | ||
|
||
这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 01.02` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 | ||
|
||
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 | ||
|
||
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 | ||
|
||
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 | ||
|