Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5ebc197

Browse files
committed
Anagram
1 parent a993e51 commit 5ebc197

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/isAnagram/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// pick a solution and insert here to run the test.
2+
3+
4+
function isAnagram(stringA, stringB) {
5+
6+
const sanitizeString = function (str) {
7+
return str.toLowerCase().replace(/[^a-z\d]/g, '').split('').sort().join('');
8+
}
9+
10+
return sanitizeString(stringA) == sanitizeString(stringB)
11+
12+
}
13+
14+
module.exports = isAnagram;

src/isAnagram/solutions.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// DIRECT COMPARISON
2+
3+
function isAnagram(stringA, stringB) {
4+
5+
const sanitizeString = function (str) {
6+
return str.toLowerCase().replace(/[^a-z\d]/g, '').split('').sort().join('');
7+
}
8+
9+
return sanitizeString(stringA) == sanitizeString(stringB)
10+
11+
}
12+
13+
// CHARACTER MAP COMPARISON
14+
15+
function isAnagram(stringA, stringB) {
16+
17+
function createCharMap(text) {
18+
let charMap = {}
19+
for (let char of text) {
20+
if (charMap.hasOwnProperty(char)) {
21+
charMap[char]++
22+
} else {
23+
charMap[char] = 1
24+
}
25+
}
26+
return charMap
27+
}
28+
29+
if (stringA.length === stringB.length) {
30+
31+
let stringAMap = createCharMap(stringA)
32+
let stringBMap = createCharMap(stringB)
33+
34+
for (let char in stringAMap) {
35+
if (stringAMap[char] !== stringBMap[char]) {
36+
return false
37+
}
38+
}
39+
40+
return true
41+
} else {
42+
return false
43+
}
44+
}

src/isAnagram/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const isAnagram = require('./index')
2+
3+
test('isAnagram is a function', () => {
4+
expect(typeof isAnagram).toEqual('function')
5+
})
6+
7+
test('"dog" is an anagram of "god"', () => {
8+
expect(isAnagram('dog', 'god')).toBeTruthy()
9+
})
10+
11+
test('"Scotchy is Scotch!" is an anagram of "Scotch is Scotchy!"', () => {
12+
expect(isAnagram('Scotchy is Scotch!', 'Scotch is Scotchy!')).toBeTruthy()
13+
})
14+
15+
test('"I do not work weekends." is not an anagram of "I do not work weekdays!"', () => {
16+
expect(isAnagram('I do not work weekends.', 'I do not work weekdays!')).toBeFalsy()
17+
})

src/searchReplace/test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)