@ParasArora/

anagrams

Nodejs

No description

fork
loading
Files
  • index.js
index.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
function anagrams(a, b){
  if(a.length !== b.length){
    // console.log('by non eq lengths')
    return "Not Anagrams";
  } 

  let strA = a.toLowerCase();
  let strB = b.toLowerCase();
  
  let strMap = {};

  for(let i = 0; i < strA.length; ++i){
  
    if(strA[i] in strMap){
      strMap[strA[i]][0]++;
    }else{
      strMap[strA[i]] = [1, 0];
    }
  }

  for(let j= 0; j< strB.length; ++j){
  
    if(strB[j] in strMap){
      strMap[strB[j]][1]++;
    }else{
      // console.log('by non existing property')
      return "Not Anagrams";
    }
  }

  for(let letter in strMap){
    c++;
    if(strMap[letter][0] !== strMap[letter][1]){
      // console.log('by non equal freq.')
      return "Not Anagrams";
    }
  }

  return "Anagrams";
  
}

anagrams("helxo", "Helxo");
// anagrams("anagram", "margana");

node v10.16.0