-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
637 changed files
with
126,605 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
_CONTAINER/_my_DS_repo/DS-n-Algos/_Extra-Practice/anagrams/index.js
This file contains 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,51 @@ | ||
// --- Directions | ||
// Check to see if two provided strings are anagrams of eachother. | ||
// One string is an anagram of another if it uses the same characters | ||
// in the same quantity. Only consider characters, not spaces | ||
// or punctuation. Consider capital letters to be the same as lower case | ||
// --- Examples | ||
// anagrams('rail safety', 'fairy tales') --> True | ||
// anagrams('RAIL! SAFETY!', 'fairy tales') --> True | ||
// anagrams('Hi there', 'Bye there') --> False | ||
|
||
function anagrams(stringA, stringB) { | ||
return cleanString(stringA) === cleanString(stringB); | ||
} | ||
|
||
function cleanString(str) { | ||
return str | ||
.replace(/[^\w]/g, '') | ||
.toLowerCase() | ||
.split('') | ||
.sort() | ||
.join(''); | ||
} | ||
|
||
module.exports = anagrams; | ||
|
||
// function anagrams(stringA, stringB) { | ||
// const aCharMap = buildCharMap(stringA); | ||
// const bCharMap = buildCharMap(stringB); | ||
// | ||
// if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) { | ||
// return false; | ||
// } | ||
// | ||
// for (let char in aCharMap) { | ||
// if (aCharMap[char] !== bCharMap[char]) { | ||
// return false; | ||
// } | ||
// } | ||
// | ||
// return true; | ||
// } | ||
// | ||
// function buildCharMap(str) { | ||
// const charMap = {}; | ||
// | ||
// for (let char of str.replace(/[^\w]/g, '').toLowerCase()) { | ||
// charMap[char] = charMap[char] + 1 || 1; | ||
// } | ||
// | ||
// return charMap; | ||
// } |
Oops, something went wrong.