mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
update242.有效的字母异位词
This commit is contained in:
@ -143,17 +143,23 @@ func isAnagram(s string, t string) bool {
|
|||||||
javaScript:
|
javaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
/**
|
||||||
|
* @param {string} s
|
||||||
|
* @param {string} t
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
var isAnagram = function(s, t) {
|
var isAnagram = function(s, t) {
|
||||||
const resSet = new Array(25).fill(0);
|
if(s.length !== t.length) return false;
|
||||||
|
const resSet = new Array(26).fill(0);
|
||||||
const base = "a".charCodeAt();
|
const base = "a".charCodeAt();
|
||||||
for(const i of s) {
|
for(const i of s) {
|
||||||
resSet[i.charCodeAt() - base]++;
|
resSet[i.charCodeAt() - base]++;
|
||||||
}
|
}
|
||||||
for(const i of t) {
|
for(const i of t) {
|
||||||
|
if(!resSet[i.charCodeAt() - base]) return false;
|
||||||
resSet[i.charCodeAt() - base]--;
|
resSet[i.charCodeAt() - base]--;
|
||||||
// if(val < 0) return false;
|
|
||||||
}
|
}
|
||||||
return resSet.every(i => !i);
|
return true;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ javaScript:
|
|||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
var canConstruct = function(ransomNote, magazine) {
|
var canConstruct = function(ransomNote, magazine) {
|
||||||
const strArr = new Array(25).fill(0),
|
const strArr = new Array(26).fill(0),
|
||||||
base = "a".charCodeAt();
|
base = "a".charCodeAt();
|
||||||
for(const s of magazine) {
|
for(const s of magazine) {
|
||||||
strArr[s.charCodeAt() - base]++;
|
strArr[s.charCodeAt() - base]++;
|
||||||
|
Reference in New Issue
Block a user