题解 | #字符串的排列#

字符串的排列

https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7

function Permutation(str)
{
    // write code here
    if (str.length <= 1) return [str]
    const result = []
    const items = str.split('')
    const current = items.shift()
    const subResult = Permutation(items.join(''))
    for (let i = 0; i <= items.length; i++) {
        for (let j = 0; j < subResult.length; j++) {
            const currentSubItem = subResult[j].split('')
            currentSubItem.splice(i, 0, current)
            const currentStr = currentSubItem.join('')
            result.push(currentStr)
        }
    }
    const res = new Set(result)
    return Array.from(res)
}
module.exports = {
    Permutation : Permutation
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务