-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidAnagram.js
More file actions
35 lines (32 loc) · 797 Bytes
/
validAnagram.js
File metadata and controls
35 lines (32 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* Given two strings s and t , write a function to determine if t is an anagram of s.
*
* Example 1:
*
* Input: s = "anagram", t = "nagaram"
* Output: true
* Example 2:
*
* Input: s = "rat", t = "car"
* Output: false
* Note:
* You may assume the string contains only lowercase alphabets.
*/
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if (s.length !== t.length) { return false;}
let charToCount = {};
for(char of s){
charToCount[char] = charToCount[char] ? charToCount[char] + 1 : 1;
}
for (char of t){
if (charToCount[char]){
charToCount[char]--;
}
}
return Object.keys(charToCount).filter(key => charToCount[key] > 0).length === 0 ;
};