-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-07-03.js
29 lines (26 loc) · 964 Bytes
/
2023-07-03.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// https://www.codewars.com/kata/587e18b97a25e865530000d8/train/javascript
function anagramCounter( wordsArray ) {
let counter = 0;
wordsArray.forEach( ( word ) => {
const regEx = new RegExp( `^[${word}]{${word.length}}$` );
wordsArray.forEach( ( checkAnagram ) => {
if ( word !== checkAnagram ) {
if ( regEx.test( checkAnagram ) ) counter++;
}
} );
} );
return counter / 2;
}
console.log( anagramCounter( [ 'dell', 'ledl', 'abc', 'cba', 'bca', 'bac', 'cab' ] ) );
// others solution
function anagramCounter2( wordsArray ) {
let numberOfAnagrams = 0;
const wordsSorted = wordsArray.map( ( word ) => word.split( '' ).sort().join( '' ).toLowerCase() );
wordsSorted.forEach( ( word, idx ) => {
for ( let i = idx; i < wordsSorted.length; i++ ) {
if ( word === wordsSorted[i] && idx !== i ) numberOfAnagrams++;
}
} );
return numberOfAnagrams;
}
console.log( anagramCounter2( [ 'dell', 'ledl', 'abc', 'cba', 'bca', 'bac', 'cab' ] ) );