Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added algorithm CheckHeterogram#1221

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
KevinLindh wants to merge3 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromKevinLindh:master
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsString/CheckHeterogram.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsString/test/CheckHeterogram.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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)
})
})

[8]ページ先頭

©2009-2025 Movatter.jp