function subAnagram(anagram, string) {
const m = anagram.length;
const n = string.length;
if (m > n) return false;
const target = anagram.split('').reduce((accum, char) => {
if (!accum[char]) {
accum[char] = 1;
} else {
accum[char] = accum[char]++;
}
return accum;
},{});
anagram.split('').forEach((char, i) => {
const c = string[i];
if (target[c]) target[c]--;
});
let discrepancy = Object.keys(target).reduce((accum, char) => accum + Math.abs(target[char]), 0);
for ( let i = m; i < n; i++ ) {
if (discrepancy === 0) {
return true;
} else {
let c = string[i-m];
if (target[c] !== undefined) {
target[c]++;
(target[c] > 0 && discrepancy++) || (discrepancy--);
}
c = string[i];
if (target[c] !== undefined) {
target[c]--;
(target[c] < 0 && discrepancy++) || (discrepancy--);
}
}
}
return discrepancy === 0;
}