diff --git a/String/CheckHeterogram.js b/String/CheckHeterogram.js new file mode 100644 index 0000000000..624c1ca216 --- /dev/null +++ b/String/CheckHeterogram.js @@ -0,0 +1,40 @@ +// A [Heterogram](https://en.wikipedia.org/wiki/Heterogram_(literature)) is a word, phrase or sentence in which no letter of the alphabet occurs more than once. + +/** + * @function checkHeterogram + * @param {string} str + * @returns {boolean} + * @description - check if a string is a heterogram + * @example - checkHeterogram('hero') => true + * @example - checkHeterogram('CoMpUtEr') => true + * @example - checkHeterogram('Great work!!') => true + * @example - checkHeterogram('Aba') => false + */ + +const checkHeterogram = (str) => { + // Check that input is a string + if (typeof str !== 'string') { + throw new TypeError('Argument not a string.') + } + + // initialize a hashmap where the letters of the string will be checked for duplicates + const letters = new Map() + + // Check all the characters in the string by looping through it + for (let i = 0; i < str.length; i++) { + // If the character is a letter check whether it is a duplicate + if (str.charAt(i).match(/[a-z]/gi)) { + // If the letter exists in the hashmap already return false else store it in the hashmap + if (letters.has(str.charAt(i).toLowerCase())) { + return false + } else { + letters.set(str.charAt(i).toLowerCase()) + } + } + } + + // Once all characters of the string have been checked return true + return true +} + +export { checkHeterogram } diff --git a/String/test/CheckHeterogram.test.js b/String/test/CheckHeterogram.test.js new file mode 100644 index 0000000000..4c036c23dc --- /dev/null +++ b/String/test/CheckHeterogram.test.js @@ -0,0 +1,31 @@ +import { checkHeterogram } from '../CheckHeterogram' + +describe('Testing checkHeterogram', () => { + it('expects to throw type error if given a non string argument', () => { + expect(() => checkHeterogram(0)).toThrow() + }) + it('expects to throw type error if given a non string argument', () => { + expect(() => checkHeterogram(true)).toThrow() + }) + it('expects to return true if the argument is a heterogram', () => { + expect(checkHeterogram('hero')).toBe(true) + }) + it('expects to return true if the given argument only contains non-alphabetic characters', () => { + expect(checkHeterogram('!!..&& ??22')).toBe(true) + }) + it('expects to return true if the given string is a heterogram which contains duplicate non-alphabetic characters', () => { + expect(checkHeterogram('C, o, m, p, u, t, e, r!!')).toBe(true) + }) + it('expects to return true if the given string is empty', () => { + expect(checkHeterogram('')).toBe(true) + }) + it('expects to return true if the given string contains multiple spaces and is a heterogram', () => { + expect(checkHeterogram('Cwm fjord bank glyphs vext quiz')).toBe(true) + }) + it('expects to return false if the given string contains duplicate letters with differing case (case insensitive)', () => { + expect(checkHeterogram('Ada')).toBe(false) + }) + it('expects to return false if the given string contains duplicate letters in different words', () => { + expect(checkHeterogram('List duplicates')).toBe(false) + }) +})

AltStyle によって変換されたページ (->オリジナル) /