Skip to content

Commit d74f454

Browse files
author
Coding Money
committed
anagrams
1 parent 5dfc684 commit d74f454

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Diff for: completed_exercises/6-anagrams.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
// }

Diff for: exercises/6-anagrams.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 anagrams(stringA, stringB) {}
12+
13+
14+
console.log(anagrams('RAIL! SAFETY!', 'fairy tales'));

0 commit comments

Comments
 (0)