| 9 | * @example - checkAnagramRegex('Eleven plus two', 'Twelve plus one') => true |
| 10 | */ |
| 11 | const checkAnagramRegex = (str1, str2) => { |
| 12 | // check that inputs are strings. |
| 13 | if (typeof str1 !== 'string' || typeof str2 !== 'string') { |
| 14 | throw new TypeError('Both arguments should be strings.') |
| 15 | } |
| 16 | |
| 17 | // If both strings have not same lengths then they can not be anagram. |
| 18 | if (str1.length !== str2.length) { |
| 19 | return false |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * str1 converted to an array and traverse each letter of str1 by reduce method |
| 24 | * reduce method return string which is empty or not. |
| 25 | */ |
| 26 | return ![...str1].reduce( |
| 27 | (str2Acc, cur) => str2Acc.replace(new RegExp(cur, 'i'), ''), // remove the similar letter from str2Acc in case-insensitive |
| 28 | str2 |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @function checkAnagramMap |