JSFiddle - React, Tailwind, and code Playground
JavaScript
let arr = [];
let permute = function(str, l, r) {
if (l === r) {
arr.push(str);
} else {
for (let i = l; i <= r; i++) {
str[l] = str[i];
str[i] = str[l];
permute(str, l + 1, r);
str[l] = str[i];
str[i] = str[l];
}
}
};
permute('aab', 0, 'aab'.length - 1);
console.log(arr);