const powerSet = function (str) {
const sorted = str.split('').sort();
const deduplicated = sorted.reduce((acc, item) => {
if (acc[acc.length - 1] === item) {
return acc;
} else {
return acc.concat(item);
}
});
let subSets = [];
const pickOrNot = (idx, subset) => {
if (idx === deduplicated.length) {
subSets.push(subset);
return;
}
pickOrNot(idx + 1, subset);
pickOrNot(idx + 1, subset + deduplicated[idx]);
};
pickOrNot(0, '');
return subSets.sort();
};
let bfs = function (node) {
let queue = [node];
const values = [];
while (queue.length > 0) {
const head = queue[0];
queue = queue.slice(1);
values.push(head.value);
head.children.forEach((child) => queue.push(child));
}
return values;
};
let Node = function (value) {
this.value = value;
this.children = [];
};
Node.prototype.addChild = function (child) {
this.children.push(child);
return child;
};
let dfs = function (node) {
let values = [node.value];
node.children.forEach((n) => {
values = values.concat(dfs(n));
});
return values;
};
let Node = function (value) {
this.value = value;
this.children = [];
};
Node.prototype.addChild = function (child) {
this.children.push(child);
return child;
};