Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added algorithm CheckHeterogram #1221

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions String/CheckHeterogram.js
Original file line number Diff line number Diff line change
@@ -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 }
31 changes: 31 additions & 0 deletions String/test/CheckHeterogram.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})