Skip to content

Commit e0e620d

Browse files
feat: add check anagram algorithm
1 parent 5c39e87 commit e0e620d

2 files changed

Lines changed: 39 additions & 225 deletions

File tree

‎String/CheckAnagram.js‎

Lines changed: 17 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,23 @@
1-
// An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive;
21
/**
3-
* @function checkAnagramRegex
4-
* @param {string} str1
5-
* @param {string} str2
6-
* @returns {boolean}
7-
* @description - check anagram with the help of Regex
8-
* @example - checkAnagramRegex('node', 'deno') => true
9-
* @example - checkAnagramRegex('Eleven plus two', 'Twelve plus one') => true
2+
* @function checkAnagram
3+
* @description An anagram is a word or phrase formed by rearranging the
4+
* letters of another word or phrase. This function checks if two strings
5+
* are anagrams of each other, ignoring case and spaces.
6+
* @param {string} str1 - The first input string
7+
* @param {string} str2 - The second input string
8+
* @returns {boolean} - True if the strings are anagrams, false otherwise
9+
* @see https://en.wikipedia.org/wiki/Anagram
10+
* @example checkAnagram("listen", "silent") => true
11+
* @example checkAnagram("hello", "world") => false
12+
* @example checkAnagram("Astronomer", "Moon starer") => true
1013
*/
11-
const checkAnagramRegex = (str1, str2) => {
12-
// check that inputs are strings.
14+
const checkAnagram = (str1, str2) => {
1315
if (typeof str1 !== 'string' || typeof str2 !== 'string') {
14-
throw new TypeError('Both arguments should be strings.')
16+
throw new TypeError('Both inputs must be strings')
1517
}
16-
17-
// If both strings have not same lengths then they can not be anagram.
18-
if (str1.length !== str2.length) {
19-
return false
20-
}
21-
22-
/**
23-
* str1 converted to an array and traverse each letter of str1 by reduce method
24-
* reduce method return string which is empty or not.
25-
*/
26-
return ![...str1].reduce(
27-
(str2Acc, cur) => str2Acc.replace(new RegExp(cur, 'i'), ''), // remove the similar letter from str2Acc in case-insensitive
28-
str2
29-
)
30-
}
31-
32-
/**
33-
* @function checkAnagramMap
34-
* @description - check anagram via using HashMap
35-
* @param {string} str1
36-
* @param {string} str2
37-
* @returns {boolean}
38-
* @example - checkAnagramMap('node', 'deno') => true
39-
* @example - checkAnagramMap('Eleven plus two', 'Twelve plus one') => true
40-
*/
41-
const checkAnagramMap = (str1, str2) => {
42-
// check that inputs are strings.
43-
if (typeof str1 !== 'string' || typeof str2 !== 'string') {
44-
throw new TypeError('Both arguments should be strings.')
45-
}
46-
47-
// If both strings have not same lengths then they can not be anagram.
48-
if (str1.length !== str2.length) {
49-
return false
50-
}
51-
52-
const str1List = Array.from(str1.toUpperCase()) // str1 to array
53-
54-
// get the occurrences of str1 characters by using HashMap
55-
const str1Occurs = str1List.reduce(
56-
(map, char) => map.set(char, map.get(char) + 1 || 1),
57-
new Map()
58-
)
59-
60-
for (const char of str2.toUpperCase()) {
61-
// if char has not exist to the map it's return false
62-
if (!str1Occurs.has(char)) {
63-
return false
64-
}
65-
66-
let getCharCount = str1Occurs.get(char)
67-
str1Occurs.set(char, --getCharCount)
68-
69-
getCharCount === 0 && str1Occurs.delete(char)
70-
}
71-
72-
return true
18+
const normalize = (str) =>
19+
str.toLowerCase().replace(/\s/g, '').split('').sort().join('')
20+
return normalize(str1) === normalize(str2)
7321
}
7422

75-
export { checkAnagramRegex, checkAnagramMap }
23+
export { checkAnagram }

‎String/test/CheckAnagram.test.js‎

Lines changed: 22 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,43 @@
1-
import { checkAnagramMap, checkAnagramRegex } from '../CheckAnagram'
1+
import { checkAnagram } from '../CheckAnagram.js'
22

3-
describe('Testing checkAnagramRegex', () => {
4-
it.each`
5-
inputOne | inputTwo
6-
${123456} | ${'abcd'}
7-
${[1, 2, 3, 4, 5, 6]} | ${'abcd'}
8-
${{ test: 'test' }} | ${'abcd'}
9-
${'abcd'} | ${123456}
10-
${'abcd'} | ${[1, 2, 3, 4, 5, 6]}
11-
${'abcd'} | ${{ test: 'test' }}
12-
`(
13-
'expects to throw the type Error given values $inputOne and $inputTwo',
14-
({ inputOne, inputTwo }) => {
15-
expect(() => checkAnagramRegex(inputOne, inputTwo)).toThrowError()
16-
}
17-
)
18-
19-
it('expects to return false if the arguments have different lengths', () => {
20-
const SUT = checkAnagramRegex('abs', 'abds')
21-
expect(SUT).toBe(false)
22-
})
23-
24-
it('expects to return false if the arguments are not anagrams', () => {
25-
const SUT = checkAnagramRegex('abcs', 'abds')
26-
expect(SUT).toBe(false)
27-
})
28-
29-
it('expects to return true if the arguments are anagrams', () => {
30-
const SUT = checkAnagramRegex('abcd', 'bcad')
31-
expect(SUT).toBe(true)
32-
})
33-
34-
it('expects to return true if the arguments of length 1 and are the same letter', () => {
35-
const SUT = checkAnagramRegex('a', 'a')
36-
expect(SUT).toBe(true)
37-
})
38-
39-
it('expects to return true if the arguments of are both empty strings', () => {
40-
const SUT = checkAnagramRegex('', '')
41-
expect(SUT).toBe(true)
42-
})
43-
44-
it('expects to return true if the arguments are anagrams with an odd length', () => {
45-
const SUT = checkAnagramRegex('abcde', 'edcab')
46-
expect(SUT).toBe(true)
47-
})
48-
49-
it('expects to return true if the arguments are anagrams with an even length', () => {
50-
const SUT = checkAnagramRegex('abcdef', 'fedcab')
51-
expect(SUT).toBe(true)
52-
})
53-
54-
it('expects to return false if either argument is an empty string while the other is not', () => {
55-
const SUT = checkAnagramRegex('', 'edcab')
56-
expect(SUT).toBe(false)
57-
const SUT2 = checkAnagramRegex('edcab', '')
58-
expect(SUT2).toBe(false)
59-
})
60-
61-
it('expects to return true if the arguments contain the same letters but have unequal case', () => {
62-
const SUT = checkAnagramRegex('ABDCE', 'abcde')
63-
expect(SUT).toBe(true)
64-
const SUT2 = checkAnagramRegex('AbCdE', 'aBCdE')
65-
expect(SUT2).toBe(true)
66-
const SUT3 = checkAnagramRegex('Eleven plus two', 'Twelve plus one')
67-
expect(SUT3).toBe(true)
68-
})
69-
70-
it('expects to return true if the arguments are anagrams and contain number characters', () => {
71-
const SUT = checkAnagramRegex('a1b2', '12ba')
72-
expect(SUT).toBe(true)
73-
})
74-
75-
it('expects to return true if the arguments are anagrams and contain space characters', () => {
76-
const SUT = checkAnagramRegex('a1 b2', '1 2ba')
77-
expect(SUT).toBe(true)
78-
})
79-
80-
it('expects to return true if the arguments are anagrams and contain punctuation characters', () => {
81-
const SUT = checkAnagramRegex('a!1b@2', '1@2ba!')
82-
expect(SUT).toBe(true)
83-
})
84-
85-
it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => {
86-
const SUT = checkAnagramRegex('ea cb', 'e cba')
87-
expect(SUT).toBe(false)
88-
})
89-
})
90-
91-
describe('Testing checkAnagramMap', () => {
92-
it.each`
93-
inputOne | inputTwo
94-
${123456} | ${'abcd'}
95-
${[1, 2, 3, 4, 5, 6]} | ${'abcd'}
96-
${{ test: 'test' }} | ${'abcd'}
97-
${'abcd'} | ${123456}
98-
${'abcd'} | ${[1, 2, 3, 4, 5, 6]}
99-
${'abcd'} | ${{ test: 'test' }}
100-
`(
101-
'expects to throw the type Error given values $inputOne and $inputTwo',
102-
({ inputOne, inputTwo }) => {
103-
expect(() => checkAnagramMap(inputOne, inputTwo)).toThrowError()
104-
}
105-
)
106-
107-
it('expects to return false if the arguments have different lengths', () => {
108-
const SUT = checkAnagramMap('abs', 'abds')
109-
expect(SUT).toBe(false)
110-
})
111-
112-
it('expects to return false if the arguments are not anagrams', () => {
113-
const SUT = checkAnagramMap('abcs', 'abds')
114-
expect(SUT).toBe(false)
115-
})
116-
117-
it('expects to return true if the arguments are anagrams', () => {
118-
const SUT = checkAnagramMap('abcd', 'bcad')
119-
expect(SUT).toBe(true)
120-
})
121-
122-
it('expects to return true if the arguments of length 1 and are the same letter', () => {
123-
const SUT = checkAnagramMap('a', 'a')
124-
expect(SUT).toBe(true)
3+
describe('checkAnagram', () => {
4+
test("'listen' and 'silent' are anagrams", () => {
5+
expect(checkAnagram('listen', 'silent')).toBe(true)
1256
})
1267

127-
it('expects to return true if the arguments of are both empty strings', () => {
128-
const SUT = checkAnagramMap('', '')
129-
expect(SUT).toBe(true)
8+
test("'hello' and 'world' are NOT anagrams", () => {
9+
expect(checkAnagram('hello', 'world')).toBe(false)
13010
})
13111

132-
it('expects to return true if the arguments are anagrams with an odd length', () => {
133-
const SUT = checkAnagramMap('abcde', 'edcab')
134-
expect(SUT).toBe(true)
12+
test("'Astronomer' and 'Moon starer' are anagrams", () => {
13+
expect(checkAnagram('Astronomer', 'Moon starer')).toBe(true)
13514
})
13615

137-
it('expects to return true if the arguments are anagrams with an even length', () => {
138-
const SUT = checkAnagramMap('abcdef', 'fedcab')
139-
expect(SUT).toBe(true)
16+
test("'triangle' and 'integral' are anagrams", () => {
17+
expect(checkAnagram('triangle', 'integral')).toBe(true)
14018
})
14119

142-
it('expects to return false if either argument is an empty string while the other is not', () => {
143-
const SUT = checkAnagramMap('', 'edcab')
144-
expect(SUT).toBe(false)
145-
const SUT2 = checkAnagramMap('edcab', '')
146-
expect(SUT2).toBe(false)
20+
test('empty strings are anagrams', () => {
21+
expect(checkAnagram('', '')).toBe(true)
14722
})
14823

149-
it('expects to return true if the arguments contain the same letters but have unequal case', () => {
150-
const SUT = checkAnagramMap('ABDCE', 'abcde')
151-
expect(SUT).toBe(true)
152-
const SUT2 = checkAnagramMap('AbCdE', 'aBCdE')
153-
expect(SUT2).toBe(true)
154-
const SUT3 = checkAnagramMap('Eleven plus two', 'Twelve plus one')
155-
expect(SUT3).toBe(true)
24+
test('case insensitive check', () => {
25+
expect(checkAnagram('Listen', 'Silent')).toBe(true)
15626
})
15727

158-
it('expects to return true if the arguments are anagrams and contain number characters', () => {
159-
const SUT = checkAnagramMap('a1b2', '12ba')
160-
expect(SUT).toBe(true)
28+
test('different lengths are NOT anagrams', () => {
29+
expect(checkAnagram('abc', 'ab')).toBe(false)
16130
})
16231

163-
it('expects to return true if the arguments are anagrams and contain space characters', () => {
164-
const SUT = checkAnagramMap('a1 b2', '1 2ba')
165-
expect(SUT).toBe(true)
32+
test("'abc' and 'cba' are anagrams", () => {
33+
expect(checkAnagram('abc', 'cba')).toBe(true)
16634
})
16735

168-
it('expects to return true if the arguments are anagrams and contain punctuation characters', () => {
169-
const SUT = checkAnagramMap('a!1b@2', '1@2ba!')
170-
expect(SUT).toBe(true)
36+
test('throws TypeError for non-string first input', () => {
37+
expect(() => checkAnagram(123, 'silent')).toThrow(TypeError)
17138
})
17239

173-
it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => {
174-
const SUT = checkAnagramMap('ea cb', 'e cba')
175-
expect(SUT).toBe(false)
40+
test('throws TypeError for non-string second input', () => {
41+
expect(() => checkAnagram('listen', 123)).toThrow(TypeError)
17642
})
17743
})

0 commit comments

Comments
 (0)