-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-anagrams.js
43 lines (35 loc) · 1.01 KB
/
14-anagrams.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
/*
Q-14
Given two strings, write a function to determine whether they are
anagrams
isAnagram("", "") = true
isAnagram("A", "A") = true
isAnagram("A", "B") = false
isAnagram("ab", "ba") = true
isAnagram("ABa", "ab") = true
PS: in below apprach ... instead of plain map ..we can create a array of length 256 (because alphabet size)
and set values to 1 and 0 to know the result.
*/
function isAnagram(input1, input2){
let result = false;
input1 = input1.toLowerCase();
input2 = input2.toLowerCase();
const cache = {};
let charCount = 0;
input1.split('').forEach( (char) => {
if(cache.hasOwnProperty(char)){
cache[char] = cache[char]+1;
}else{
cache[char] = 1;
charCount++;
}
});
for(let i=0; i<input2.length;i++){
const char2 = input2.charAt(i);
if(cache.hasOwnProperty(char2)){
cache[char2] = cache[char2]-1;
charCount--;
}
}
return charCount == 0;
}