Skip to content

Commit e3153bc

Browse files
committed
242. 有效的字母异位词 (二刷)
1 parent 92c26f7 commit e3153bc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

vs-lt/242.有效的字母异位词.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=242 lang=javascript
3+
*
4+
* [242] 有效的字母异位词
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @param {string} t
11+
* @return {boolean}
12+
*/
13+
var isAnagram = function (s, t) {
14+
if (s.length !== t.length) return false;
15+
16+
const sMap = Array(26).fill(0);
17+
for (let i = 0; i < s.length; i++) {
18+
const index = s[i].charCodeAt() - 97;
19+
sMap[index]++;
20+
}
21+
for (let i = 0; i < t.length; i++) {
22+
const index = t[i].charCodeAt() - 97;
23+
sMap[index]--;
24+
if (sMap[index] < 0) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
};
30+
// @lc code=end
31+

0 commit comments

Comments
 (0)