-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.js
More file actions
52 lines (39 loc) · 1.17 KB
/
anagram.js
File metadata and controls
52 lines (39 loc) · 1.17 KB
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
52
/* Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different
word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 104
s and t consist of lowercase English letters.
*/
//SOLUTION 1 => O(n)
let isAnagram1 = function (s, t) {
if (s.length !== t.length) {
return false;
}
let frequencyCounter1 = {};
let frequencyCounter2 = {};
for (let letter of s) {
frequencyCounter1[letter] = (frequencyCounter1[letter] || 0) + 1;
}
for (let letter of t) {
frequencyCounter2[letter] = (frequencyCounter2[letter] || 0) + 1;
}
for (let letter in frequencyCounter1) {
if (!(letter in frequencyCounter2)) {
return false;
}
if (frequencyCounter1[letter] !== frequencyCounter2[letter]) {
return false;
}
}
return true;
};
console.log(isAnagram1("anagram", "nagaram"));
console.log(isAnagram1("anagram", "bagaram"));
console.log(isAnagram1("anagram", "baaram"));