Brace Expansion
Description
null
Solution(javascript)
/**
* @param {string} S
* @return {string[]}
*/
var expand = function(S) {
const arr = []
let tempArr = []
let tempStr = ""
let start = false
for(let c of S) {
if(c === "{") {
start = true
if(tempStr.length > 0) {
arr.push([tempStr])
tempStr = ""
}
} else if(c === "}") {
start = false
if(tempArr.length > 0) {
arr.push(tempArr)
tempArr = []
}
}else if(c===",") {
continue
} else {
if(start) {
tempArr.push(c)
} else {
tempStr += c
}
}
}
if(tempStr.length > 0) {
arr.push([tempStr])
}
let result = []
const aux = (index=0, current = "") => {
if(index >= arr.length) {
result.push(current)
return
}
arr[index].forEach(s => aux(index+1, current + s))
}
aux()
return result.sort()
};