-
Notifications
You must be signed in to change notification settings - Fork 44
/
anagram.js
71 lines (61 loc) · 1.84 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function anagram(s){
/* Find the difference between the frequencies of each character in string2 with its corresponding frequency in string1
and only if the difference is positive, add it to the final result i.e. if the no. of times the character
occuring in the second string is more than in the first string. */
if(s.length%2!=0){
return -1;
}
else{
var arr= s.split("");
var startIndex= arr.length/2;
var obj1 =new Object();
var obj2= new Object();
var mySet1= new Set();
var mySet= new Set();
for(var i=0; i<startIndex; i++){
mySet1.add(arr[i]);
}
for(var i=startIndex; i<arr.length; i++){
mySet.add(arr[i]);
}
for(var item of mySet1){
var freq=0;
for(var i=0; i<startIndex; i++){
if(item == arr[i]){
freq++;
}
}
obj1[item]= freq;
}
for(var item of mySet){
var freq=0;
for(var i=startIndex; i<arr.length; i++){
if(item == arr[i]){
freq++;
}
}
obj2[item]= freq;
}
var res=0;
for(var prop in obj2){
if(obj1.hasOwnProperty(prop)){
var diff= obj2[prop]-obj1[prop];
if(diff>0){
res= res + (diff);
}
}
else{
res= res + obj2[prop];
}
}
return res;
}
}
function main() {
var q = parseInt(readLine());
for(var a0 = 0; a0 < q; a0++){
var s = readLine();
var result = anagram(s);
process.stdout.write("" + result + "\n");
}
}