Skip to content

Commit cd62aa8

Browse files
committed
242. 有效的字母异位词
1 parent cd787ce commit cd62aa8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
|231|[2 的幂](https://leetcode.cn/problems/power-of-two/)|[JavaScript](./algorithms/power-of-two.js)|Easy|
7171
|234|[回文链表](https://leetcode-cn.com/problems/palindrome-linked-list/)|[JavaScript](./algorithms/palindrome-linked-list.js)|Easy|
7272
|237|[删除链表中的节点](https://leetcode-cn.com/problems/delete-node-in-a-linked-list/)|[JavaScript](./algorithms/delete-node-in-a-linked-list.js)|Easy|
73+
|242|[有效的字母异位词](https://leetcode.cn/problems/valid-anagram/)|[JavaScript](./algorithms/valid-anagram.js)|Easy|
7374
|257|[二叉树的所有路径](https://leetcode.cn/problems/binary-tree-paths/)|[JavaScript](./algorithms/binary-tree-paths.js)|Easy|
7475
|283|[移动零](https://leetcode.cn/problems/move-zeroes/)|[JavaScript](./algorithms/move-zeroes.js)|Easy|
7576
|303|[区域和检索 - 数组不可变](https://leetcode.cn/problems/range-sum-query-immutable/)|[JavaScript](./algorithms/range-sum-query-immutable.js)|Easy|

algorithms/valid-anagram.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function (s, t) {
7+
// 数组哈希计数
8+
const countArr = Array(26).fill(0);
9+
10+
if (s.length !== t.length) {
11+
return false;
12+
}
13+
14+
for (let char of s) {
15+
const index = char.charCodeAt() - "a".charCodeAt();
16+
countArr[index]++;
17+
}
18+
19+
for (let char of t) {
20+
const index = char.charCodeAt() - "a".charCodeAt();
21+
countArr[index]--;
22+
if (countArr[index] < 0) {
23+
return false;
24+
}
25+
}
26+
27+
return true;
28+
};

0 commit comments

Comments
 (0)