-
Notifications
You must be signed in to change notification settings - Fork 2
/
anagram.js
47 lines (43 loc) · 1.43 KB
/
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
/*
Given two strings, determine if second string is the anagram of first
An anagram is a word formed by rearranging letters of another such as cinema from iceman
validAnagram('', '') // true
validAnagram('cinema', 'iceman') // true
validAnagram('anagram', 'nagaram') // true
validAnagram('aaz', 'zza') // false
validAnagram('rat', 'car') // false
validAnagram('awesome', 'awesom') // false
*/
// Frequency counter ---> O(3n)
function validAnagram(str1='', str2=''){
if(str1.length !== str2.length) return false;
const hash1 = {};
const hash2 = {};
for(i in str1) { hash1[str1[i]] ? hash1[str1[i]] += 1 : hash1[str1[i]] = 1 };
for(i in str2) { hash2[str2[i]] ? hash2[str2[i]] += 1 : hash2[str2[i]] = 1 };
console.log(hash1,hash2);
for(let key in hash1){
if(!hash2[key]) return false;
if(hash1[key] !== hash2[key]) return false;
}
return true;
}
// optimised ---> O(2n)
function validAnagram(str1='', str2=''){
if(str1.length !== str2.length) return false;
const hash = {};
for(var i in str1) { hash[str1[i]] ? hash[str1[i]] += 1 : hash[str1[i]] = 1 };
for( var i in str2){
if(!hash[str2[i]]){
return false;
}
hash[str2[i]] -= 1;
}
return true;
}
console.log(validAnagram('', ''));
console.log(validAnagram('cinema', 'iceman'));
console.log(validAnagram('anagram', 'nagaram'));
console.log(validAnagram('aaz', 'zza'));
console.log(validAnagram('rat', 'car'));
console.log(validAnagram('awesome', 'awesom'));