Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 638ac51

Browse files
🐳(string): 242. 有效的字母异位词
补充 sort 解法
1 parent cb91531 commit 638ac51

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

‎docs/data-structure/string/README.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ class Solution(object):
11091109

11101110
[原题链接](https://leetcode-cn.com/problems/valid-anagram/)
11111111

1112-
### 思路
1112+
### 解法一:哈希
11131113

11141114
- 统计 s 和 t 中所有字母出现的次数
11151115
- 判断是否相同
@@ -1142,6 +1142,22 @@ class Solution(object):
11421142
return True
11431143
```
11441144

1145+
- 时间复杂度:O(n)
1146+
- 空间复杂度:O(n)
1147+
1148+
### 解法二:排序
1149+
1150+
```python
1151+
class Solution:
1152+
def isAnagram(self, s: str, t: str) -> bool:
1153+
s = sorted(s)
1154+
t = sorted(t)
1155+
return s == t
1156+
```
1157+
1158+
- 时间复杂度:O(nlogn)
1159+
- 空间复杂度:O(1)
1160+
11451161
### 顺便复习 Python 字典
11461162

11471163
创建字典

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /