-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path242. Valid Anagram.js
51 lines (40 loc) · 1.05 KB
/
242. Valid Anagram.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*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.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?*/
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
let len = s.length, hash = {}
if (s.length !== t.length) return false
for (let i = 0; i < len; i++) {
hash[s[i]] = (hash[s[i]] || 0) + 1
hash[t[i]] = (hash[t[i]] || 0) - 1
}
for (char in hash) {
if (hash[char] !== 0) return false
}
return true
}
var isAnagram = function(s, t) {
if (s.length !== t.length) return false
let count = Array(26).fill(0)
for (let i = 0; i < s.length; i++) {
count[s.charCodeAt(i) - 97] += 1
count[t.charCodeAt(i) - 97] -= 1
}
for (let i = 0; i < 26; i++) {
if (count[i] !== 0) return false
}
return true
}