File tree Expand file tree Collapse file tree 2 files changed +103
-0
lines changed Expand file tree Collapse file tree 2 files changed +103
-0
lines changed Original file line number Diff line number Diff line change
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('rail safety', 'fairy tales') should return True
8
+ // anagrams('RAIL! SAFETY!', 'fairy tales') should return True
9
+ // anagrams('Hi there', 'Bye there') should return False
10
+
11
+ // Solution No.1
12
+
13
+ function anagrams ( string1 , string2 ) {
14
+ let charMap1 = buildCharMap ( string1 ) ;
15
+ let charMap2 = buildCharMap ( string2 ) ;
16
+
17
+ if ( Object . keys ( charMap1 ) . length !== Object . keys ( charMap2 ) . length ) {
18
+ return false ;
19
+ }
20
+
21
+ for ( let char in charMap1 ) {
22
+ if ( charMap1 [ char ] !== charMap2 [ char ] ) {
23
+ return false ;
24
+ }
25
+ }
26
+
27
+ return true ;
28
+ }
29
+
30
+ function buildCharMap ( str ) {
31
+ let charMap = { } ;
32
+
33
+ for ( let char of str . replace ( / [ ^ \w ] / g, '' ) . toLowerCase ( ) ) {
34
+ charMap [ char ] = charMap [ char ] + 1 || 1 ;
35
+ }
36
+
37
+ return charMap ;
38
+ }
39
+
40
+
41
+ // Solution No. 2
42
+
43
+ function anagrams ( string1 , string2 ) {
44
+ return cleanString ( string1 ) === cleanString ( string2 ) ;
45
+ }
46
+
47
+ function cleanString ( str ) {
48
+ return str . replace ( / [ ^ \w ] / g, '' ) . toLowerCase ( ) . split ( '' ) . sort ( ) . join ( '' ) ;
49
+ }
Original file line number Diff line number Diff line change
1
+ # --- Directions
2
+
3
+ Check to see if two provided strings are anagrams of eachother.
4
+ One string is an anagram of another if it uses the same characters
5
+ in the same quantity. Only consider characters, not spaces
6
+ or punctuation. Consider capital letters to be the same as lower case
7
+
8
+ # --- Examples
9
+ * anagrams('rail safety', 'fairy tales') should return True
10
+ * anagrams('RAIL! SAFETY!', 'fairy tales') should return True
11
+ * anagrams('Hi there', 'Bye there') should return False
12
+
13
+
14
+ # --- Solution
15
+
16
+ Solution No 1
17
+
18
+ function anagrams(string1, string2) {
19
+ let charMap1 = buildCharMap(string1);
20
+ let charMap2 = buildCharMap(string2);
21
+
22
+ if(Object.keys(charMap1).length !== Object.keys(charMap2).length){
23
+ return false;
24
+ }
25
+
26
+ for (let char in charMap1) {
27
+ if(charMap1[char] !== charMap2[char]) {
28
+ return false;
29
+ }
30
+ }
31
+
32
+ return true;
33
+ }
34
+
35
+ function buildCharMap(str) {
36
+ let charMap = {};
37
+
38
+ for (let char of str.replace(/[^\w]/g, '').toLowerCase()) {
39
+ charMap[char] = charMap[char] + 1 || 1;
40
+ }
41
+
42
+ return charMap;
43
+ }
44
+
45
+
46
+ Solution No. 2
47
+
48
+ function anagrams(string1, string2) {
49
+ return cleanString(string1) === cleanString(string2);
50
+ }
51
+
52
+ function cleanString(str) {
53
+ return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
54
+ }
You can’t perform that action at this time.
0 commit comments