Share a strange programming algorithm question I saw recently in Turing - generate multiple pairs of parentheses that conform to the rules

Topic: According to the input integer Int n (1<n<8) as the total number of brackets, and output the array string[] of all possible combinations of brackets.

Example 1

input: n=1
output:["()"]

Example 2

input: n=3
output:["((()))", "(()())", "(())()", "()(())", "()()()"]

function handler(n) {
    
    
    let res;
    //	code here...

    return res;
}

The reason why this question is strange is that the programming algorithm questions I saw before are also about sorting brackets, but that question provides a string containing multiple brackets, such as "(()())))) ()"
requires you to figure out how many half-brackets need to be added to make all the brackets a correct pair of left and right brackets.
This kind of bracket sorting exists, mainly for automatic completion of left and right tags in HTML editors. But I can't think of any use for the question written above. Is it used to pre-calculate the possible sorting when dragging blocks?
And I spent half an hour on this question only thinking of inserting values ​​into strings, but I haven't figured out what rules to use to insert them. During the test, I completed two algorithm questions in one hour, so I automatically submitted overtime and couldn't answer.
Here is the solution that I spent another day and night (syntax: javascript):

/*
第一种思路是打散字符串插值
将字符串A插入到长度为L的字符串B中所有字符的前后间隙位置中得到Array(L+1)个可能性结果,之后再将结果去重得到本次真正的结果。
之后根据输入值循环n次,得到最终结果。
其中字符串A="()",字符串B就是上一次得到的n-1结果数组[字符串B1,字符串B2],进行迭代得到的。
 */
function handler(n) {
    
    
    //  code here...
    let res = [""];
    //  获取所有插值可能性
    const single = (str = "", ins = "()") => {
    
    
        const strArr = [...str.split(""), ""];  //  数组最后一位添加任意项,确保前后空隙都会插入值
        return Array.from(new Set(strArr.map((noMatter, index) => {
    
    
            let _arr = Array.from(strArr);
            _arr.splice(index, 0, ins);
            return _arr.join("");
        })));
    };
    //  根据输入值执行n次插入
    while (n > 0) {
    
    
        res = res.reduce((prev, item) => Array.from(new Set([...prev, ...single(item)])), []);
        n--;
    }
    return res;
}

/*
第二种思路是先转换为数值单位进行叠加
是先将字符值"()"视为一个数值单位1,之后每次进行是间隙和自身都叠加值,第一次是[1],第二次是[[1,1],[2]],第三次是[[1,1,1],[2,1],[1,2],[3]],最后才根据单位数量叠加数量重新替换为字符值,例如2="(())",3="((()))"。
本质上这种方法没有脱离第一种思路,只是少了迭代过程中数组和字符串之间频繁来回转换的行为,只需要在首尾各进行一次转换即可,所以不再放具体代码,感兴趣的可以自行实现。
 */

Complete sample code

Copy and save the following code as a local file filename.js, and then enter and run it on the command line node filename.jsto perform input and output tests:

//	@@filename.js
const readline = require("readline");
const insReadline = readline.createInterface({
    
    
    input: process.stdin,
    output: process.stdout
});

function showQuestion(question = "Please input the number of brackets you want, 0<number<8 \n", _handler = handler) {
    
    
    insReadline.question(question, input => {
    
    
        try {
    
    
            if (input >= 8 || input < 1) {
    
    
                throw new Error("input should be in [1,8].");
            }
            console.log("result:" + JSON.stringify(_handler(input)));
        } catch (err) {
    
    
            console.log(err);
        }
        console.log("End...");
        showQuestion();
    });
}

// 使用第一种思路:打散字符串插值
function handler(n) {
    
    
    //  code here...
    let res = [""];
    //  获取所有插值可能性
    const single = (str = "", ins = "()") => {
    
    
        const strArr = [...str.split(""), ""];  //  数组最后一位添加任意项,确保前后空隙都会插入值
        return Array.from(new Set(strArr.map((noMatter, index) => {
    
    
            let _arr = Array.from(strArr);
            _arr.splice(index, 0, ins);
            return _arr.join("");
        })));
    };
    //  根据输入值执行n次插入
    while (n > 0) {
    
    
        res = res.reduce((prev, item) => Array.from(new Set([...prev, ...single(item)])), []);
        n--;
    }
    return res;
}

//  don't touch
showQuestion();

The above is the problem, problem-solving ideas and code implementation of this programming algorithm problem. Welcome to comment below if you have a better solution~~

Guess you like

Origin blog.csdn.net/u013102711/article/details/128194351