Skip to content

Commit 8a2dd9c

Browse files
committed
--update: another way to check anagrams
1 parent 2093bb7 commit 8a2dd9c

File tree

2 files changed

+88
-33
lines changed

2 files changed

+88
-33
lines changed

src/anagrams/anagrams.test.js

+71-31
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,79 @@
1-
const { checkAnagrams } = require('.');
1+
const { checkAnagrams, checkAnagramUsingHelpers } = require('.');
22

33
describe('Anagrams', () => {
4-
it('Should return TRUE for `rail safety` & `fairy tales`', () => {
5-
expect(
6-
checkAnagrams({
7-
firstString: 'rail safety',
8-
secondString: 'fairy tales',
9-
}),
10-
).toBe(true);
11-
});
4+
describe('Using cutom methods and character map', () => {
5+
it('Should return TRUE for `rail safety` & `fairy tales`', () => {
6+
expect(
7+
checkAnagrams({
8+
firstString: 'rail safety',
9+
secondString: 'fairy tales',
10+
}),
11+
).toBe(true);
12+
});
1213

13-
it('Should return TRUE for `FAIRY tales` & `rail SAFETY`', () => {
14-
expect(
15-
checkAnagrams({
16-
firstString: 'FAIRY tales',
17-
secondString: 'rail SAFETY',
18-
}),
19-
).toBe(true);
20-
});
14+
it('Should return TRUE for `FAIRY tales` & `rail SAFETY`', () => {
15+
expect(
16+
checkAnagrams({
17+
firstString: 'FAIRY tales',
18+
secondString: 'rail SAFETY',
19+
}),
20+
).toBe(true);
21+
});
22+
23+
it('Should return FALSE for `Hello World` & `Bye`', () => {
24+
expect(
25+
checkAnagrams({
26+
firstString: 'Hello World',
27+
secondString: 'Bye',
28+
}),
29+
).toBe(false);
30+
});
2131

22-
it('Should return FALSE for `Hello World` & `Bye`', () => {
23-
expect(
24-
checkAnagrams({
25-
firstString: 'Hello World',
26-
secondString: 'Bye',
27-
}),
28-
).toBe(false);
32+
it('Should ignore special characters', () => {
33+
expect(
34+
checkAnagrams({
35+
firstString: 'hello world!!',
36+
secondString: 'hello - world',
37+
}),
38+
).toBe(true);
39+
});
2940
});
3041

31-
it('Should ignore special characters', () => {
32-
expect(
33-
checkAnagrams({
34-
firstString: 'hello world!!',
35-
secondString: 'hello - world',
36-
}),
37-
).toBe(true);
42+
describe('Using in built methods and sorting', () => {
43+
it('Should return TRUE for `rail safety` & `fairy tales`', () => {
44+
expect(
45+
checkAnagramUsingHelpers({
46+
firstString: 'rail safety',
47+
secondString: 'fairy tales',
48+
}),
49+
).toBe(true);
50+
});
51+
52+
it('Should return TRUE for `FAIRY tales` & `rail SAFETY`', () => {
53+
expect(
54+
checkAnagramUsingHelpers({
55+
firstString: 'FAIRY tales',
56+
secondString: 'rail SAFETY',
57+
}),
58+
).toBe(true);
59+
});
60+
61+
it('Should return FALSE for `Hello World` & `Bye`', () => {
62+
expect(
63+
checkAnagramUsingHelpers({
64+
firstString: 'Hello World',
65+
secondString: 'Bye',
66+
}),
67+
).toBe(false);
68+
});
69+
70+
it('Should ignore special characters', () => {
71+
expect(
72+
checkAnagramUsingHelpers({
73+
firstString: 'hello world!!',
74+
secondString: 'hello - world',
75+
}),
76+
).toBe(true);
77+
});
3878
});
3979
});

src/anagrams/index.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const pattern = /[^\w]/g;
2+
13
function createCharMap(str) {
24
const charMap = {};
3-
const sanitizedString = str.replace(/[^\w]/g, '').toLowerCase();
5+
const sanitizedString = str.replace(pattern, '').toLowerCase();
46
sanitizedString.split('').forEach((char) => {
57
if (!charMap[char]) {
68
charMap[char] = 1;
@@ -12,6 +14,15 @@ function createCharMap(str) {
1214
return charMap;
1315
}
1416

17+
function sanitizeAndSortString(str) {
18+
return str
19+
.replace(pattern, '')
20+
.toLowerCase()
21+
.split('')
22+
.sort()
23+
.join('');
24+
}
25+
1526
function checkAnagrams({ firstString, secondString }) {
1627
const charMapFirst = createCharMap(firstString);
1728
const charMapSecond = createCharMap(secondString);
@@ -30,7 +41,11 @@ function checkAnagrams({ firstString, secondString }) {
3041
return true;
3142
}
3243

44+
function checkAnagramUsingHelpers({ firstString, secondString }) {
45+
return sanitizeAndSortString(firstString) === sanitizeAndSortString(secondString);
46+
}
47+
3348
module.exports = {
3449
checkAnagrams,
35-
createCharMap,
50+
checkAnagramUsingHelpers,
3651
};

0 commit comments

Comments
 (0)