Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
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:masterChoose a base branch fromKevinLindh:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.