The task:
Given a word W and a string S, find all starting indices in S which are anagrams of W.
For example, given that W is "ab", and S is "abxaba", return 0, 3, and 4.
My solution:
const anagramOccurrencesOf = (w, s) => {
const occurrencesCopy = [];
let sCopy = s.slice(0);
let index = 0;
while (sCopy.length && index !== -1) {
index = sCopy.indexOf(w);
if (index === -1) { break; }
occurrencesCopy.push((occurrencesCopy[occurrencesCopy.length - 1] + index + 1 || 0));
sCopy = sCopy.slice(index + 1);
}
const occurrencesReverse = [];
let sReverse = s.split('').reverse().join('');
index = 0;
while (sReverse.length && index !== -1) {
index = sReverse.indexOf(w);
if (index === -1) { break; }
occurrencesReverse.push( (occurrencesReverse[occurrencesReverse.length - 1] - w.length - index + 1) || s.length - w.length);
sReverse = sReverse.slice(index + 1);
}
return [...occurrencesCopy, ...occurrencesReverse].sort((a,b) => a -b);
};
console.log(anagramOccurrencesOf("ab", "abxaba"));
EDIT: The solution above would find indexes of palindromes. The solution below should find indexes of anagrams:
const sortAlphabetically = x => x.toLowerCase().split('').sort().join('')
const getIndexOfAnagramIn = (s, w) => {
const wSorted = sortAlphabetically(w);
const result = []
for (let i = 0; i < s.length + 1 - w.length; i++) {
if (sortAlphabetically(s.slice(i, i + w.length)) === wSorted) {
result.push(i);
}
}
return result;
}
console.log(getIndexOfAnagramIn('abxaba', 'ab'));
abc
there are 6 anagrams. \$\endgroup\$0, 1, 3, 4, 5
? \$\endgroup\$ba
, but the 1st index would also be when backwards. \$\endgroup\$