mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
chore: merge "Tests check anagrams (#693)"
* add test cases for checkAnagram function to cover additional inputs and edge cases * adjust spacing between tests to be more consistent with other files * update CheckAnagram to return boolean value instead of string * add a reference link and definition of Anagram to CheckAnagram documentation
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
// 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 case sensitive; i.e. Aba and aba is not a anagram.
|
// Anagram check is case sensitive; i.e. Aba and aba is not a anagram.
|
||||||
// inputs are strings i.e. str1 and str2
|
// inputs are strings i.e. str1 and str2
|
||||||
const checkAnagram = (str1, str2) => {
|
const checkAnagram = (str1, str2) => {
|
||||||
@ -8,7 +9,7 @@ const checkAnagram = (str1, str2) => {
|
|||||||
|
|
||||||
// If both strings have not same lengths then they can not be anagram.
|
// If both strings have not same lengths then they can not be anagram.
|
||||||
if (str1.length !== str2.length) {
|
if (str1.length !== str2.length) {
|
||||||
return 'Not anagrams'
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use hashmap to keep count of characters in str1
|
// Use hashmap to keep count of characters in str1
|
||||||
@ -28,9 +29,8 @@ const checkAnagram = (str1, str2) => {
|
|||||||
for (let i = 0; i < str2.length; i++) {
|
for (let i = 0; i < str2.length; i++) {
|
||||||
let previousCount = 0
|
let previousCount = 0
|
||||||
// if str1CharCount has no key for str2[i] then not anagram.
|
// if str1CharCount has no key for str2[i] then not anagram.
|
||||||
if (!str1CharCount.has(str2[i])) {
|
if (!str1CharCount.has(str2[i])) return false
|
||||||
return 'Not anagrams'
|
|
||||||
}
|
|
||||||
previousCount = str1CharCount.get(str2[i])
|
previousCount = str1CharCount.get(str2[i])
|
||||||
str1CharCount.set(str2[i], previousCount - 1)
|
str1CharCount.set(str2[i], previousCount - 1)
|
||||||
}
|
}
|
||||||
@ -38,10 +38,10 @@ const checkAnagram = (str1, str2) => {
|
|||||||
// Now check if all entries in hashmap has zeros.
|
// Now check if all entries in hashmap has zeros.
|
||||||
|
|
||||||
for (const key in str1CharCount) {
|
for (const key in str1CharCount) {
|
||||||
if (str1CharCount[key] !== 0) { return 'Not anagrams' }
|
if (str1CharCount[key] !== 0) return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'Anagrams'
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export { checkAnagram }
|
export { checkAnagram }
|
||||||
|
@ -16,16 +16,73 @@ describe('checkAnagram', () => {
|
|||||||
expect(SUT).toBe('Not string(s)')
|
expect(SUT).toBe('Not string(s)')
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
it('expects to return "Not anagram" if the arguments have different lengths', () => {
|
|
||||||
|
it('expects to return false if the arguments have different lengths', () => {
|
||||||
const SUT = checkAnagram('abs', 'abds')
|
const SUT = checkAnagram('abs', 'abds')
|
||||||
expect(SUT).toBe('Not anagrams')
|
expect(SUT).toBe(false)
|
||||||
})
|
})
|
||||||
it('expects to return "Not anagram" if the arguments are not anagrams', () => {
|
|
||||||
|
it('expects to return false if the arguments are not anagrams', () => {
|
||||||
const SUT = checkAnagram('abcs', 'abds')
|
const SUT = checkAnagram('abcs', 'abds')
|
||||||
expect(SUT).toBe('Not anagrams')
|
expect(SUT).toBe(false)
|
||||||
})
|
})
|
||||||
it('expects to return "Anagram" if the arguments are anagram', () => {
|
|
||||||
|
it('expects to return true if the arguments are anagrams', () => {
|
||||||
const SUT = checkAnagram('abcd', 'bcad')
|
const SUT = checkAnagram('abcd', 'bcad')
|
||||||
expect(SUT).toBe('Anagrams')
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true if the arguments of length 1 and are the same letter', () => {
|
||||||
|
const SUT = checkAnagram('a', 'a')
|
||||||
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true if the arguments of are both empty strings', () => {
|
||||||
|
const SUT = checkAnagram('', '')
|
||||||
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true if the arguments are anagrams with an odd length', () => {
|
||||||
|
const SUT = checkAnagram('abcde', 'edcab')
|
||||||
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true if the arguments are anagrams with an even length', () => {
|
||||||
|
const SUT = checkAnagram('abcdef', 'fedcab')
|
||||||
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return false if either argument is an empty string while the other is not', () => {
|
||||||
|
const SUT = checkAnagram('', 'edcab')
|
||||||
|
expect(SUT).toBe(false)
|
||||||
|
const SUT2 = checkAnagram('edcab', '')
|
||||||
|
expect(SUT2).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return false if the arguments contain the same letters but have unequal case', () => {
|
||||||
|
const SUT = checkAnagram('ABDCE', 'abcde')
|
||||||
|
expect(SUT).toBe(false)
|
||||||
|
const SUT2 = checkAnagram('AbCdE', 'aBCdE')
|
||||||
|
expect(SUT2).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true if the arguments are anagrams and contain number characters', () => {
|
||||||
|
const SUT = checkAnagram('a1b2', '12ba')
|
||||||
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true if the arguments are anagrams and contain space characters', () => {
|
||||||
|
const SUT = checkAnagram('a1 b2', '1 2ba')
|
||||||
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true if the arguments are anagrams and contain punctuation characters', () => {
|
||||||
|
const SUT = checkAnagram('a!1b@2', '1@2ba!')
|
||||||
|
expect(SUT).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => {
|
||||||
|
const SUT = checkAnagram('ea cb', 'e cba')
|
||||||
|
expect(SUT).toBe(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user