1
+ // --- Directions
2
+ // Check to see if two provided strings are anagrams of eachother.
3
+ // One string is an anagram of another if it uses the same characters
4
+ // in the same quantity. Only consider characters, not spaces
5
+ // or punctuation. Consider capital letters to be the same as lower case
6
+ // --- Examples
7
+ // anagrams('coding money', 'money coding') --> True
8
+ // anagrams('RAIL! SAFETY!', 'fairy tales') --> True
9
+ // anagrams('Hi there', 'Bye there') --> False
10
+
11
+ function cleanStr ( str ) {
12
+ return str . toLowerCase ( ) . replace ( / [ \W ] / g, '' ) . split ( '' ) . sort ( ) . join ( '' )
13
+ }
14
+ function anagrams ( stringA , stringB ) {
15
+
16
+ return cleanStr ( stringA ) === cleanStr ( stringB )
17
+
18
+ }
19
+
20
+
21
+ console . log ( anagrams ( 'RAIL! SAFETY!' , 'fairy tales' ) ) ;
22
+
23
+
24
+ // function charMap(str){
25
+ // const charmap = {}
26
+ // str = str.toLowerCase().replace(/[\W]/g,'')
27
+ // for(let char of str){
28
+ // charmap[char] = ++charmap[char] || 1
29
+ // }
30
+ // return charmap
31
+ // }
32
+
33
+ // function anagrams(stringA, stringB) {
34
+ // //Step 1: Build Char Map for stringA
35
+ // const charmapA = charMap(stringA)
36
+
37
+ // //Step 2: Build Char Map for stringB
38
+ // const charmapB = charMap(stringB)
39
+
40
+ // //Step 3: Compare each character in the both the Char Maps
41
+ // if(Object.keys(charmapA).length !== Object.keys(charmapB).length) return false
42
+
43
+ // for(let key in charmapA){
44
+ // if(charmapA[key]!== charmapB[key]) return false
45
+ // }
46
+
47
+ // return true
48
+
49
+ // }
0 commit comments