leetcode刷题笔记301题 删除无效的括号

leetcode刷题笔记301题 删除无效的括号

源地址:301. 删除无效的括号

问题描述:

删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。

说明: 输入可能包含了除 ( 和 ) 以外的字符。

示例 1:

输入: "()())()"
输出: ["()()()", "(())()"]
示例 2:

输入: "(a)())()"
输出: ["(a)()()", "(a())()"]
示例 3:

输入: ")("
输出: [""]

//合法括号构成需满足至少以下两个条件:
//1.左括号数 == 右括号数
//2.任意前缀 左括号数 >= 右括号数
import scala.collection.mutable.ListBuffer
object Solution {
    def removeInvalidParentheses(s: String): List[String] = {
        //res:结果, l: 左括号比右括号多的个数 r:未匹配的右括号数 
        var res = ListBuffer[String]()
        var l = 0
        var r = 0

        for (x <- s) {
            if (x == '(') l += 1
            else if (x == ')'){
                if (l == 0) r += 1
                else l -= 1
            }
        }
        dfs(s, 0, "", 0, l, r)

        def dfs(s: String, u: Int, path: String, cnt: Int, l: Int, r: Int): Unit = {
            //遍历结束
            if (u == s.length) {
                //println("path: " + path)
                //左右括号数相等
                if (cnt == 0) {
                   //println("cnt: " + cnt)
                   res.append(path) 
                }
                return
            }

            //当前字符非左右括号直接添加
            if (s(u) != '(' && s(u) != ')') dfs(s, u+1, path+s(u), cnt, l, r)
            //左侧括号处理
            else if (s(u) == '('){
                var cl = l
                var cPath = path
                //var cr = r
                var cCnt = cnt
                var k = u 
                //处理连续的左括号
                while(k < s.length && s(k) == '(') k += 1
                cl -= k - u
                //println("k-u: " + (k-u))
                for (i <- (0 to k-u).reverse) {
                    println("i: " + i)
                    //仍有未匹配的左括号需要处理
                    if (cl >= 0) dfs(s, k, cPath, cCnt, cl, r)
                    cPath += '('
                    cCnt += 1
                    cl += 1
                }  
            } else if (s(u) == ')') {
                //var cl = l
                var cr = r
                var cCnt = cnt
                var cPath = path
                var k = u 
                while(k < s.length && s(k) == ')') k += 1
                cr -= k - u
                for (i <- (0 to k-u).reverse) {
                    if (cCnt >=0 &&cr >= 0) dfs(s, k, cPath, cCnt, l, cr)
                    cPath += ')'
                    cCnt -= 1
                    cr += 1
                }             
            }
        }

        return res.toList
    }
}
import "fmt"

var res = []string{}

func removeInvalidParentheses(s string) []string {
    res = []string{}
    left := 0
    right := 0
    for _, val := range s {
        if val == '(' {
            left += 1
        }else if val == ')' {
            if left == 0 {
                right += 1  
            }else {
                left -= 1
            }
        }
    }
    dfs(s, 0, "", 0, left, right)
    return res
}

func dfs(s string, u int, path string, cnt int, left int, right int) {
    if u == len(s) {
        //fmt.Printf("u: %d, len(s):%d\n", u, len(s))
        if cnt == 0 {
            //fmt.Printf("path: %s\n", path) 
            res = append(res, path)
        }
        return
    } 

    if s[u] != '(' && s[u] != ')'{
        dfs(s, u+1, path+string(s[u]), cnt, left, right)
    } else if s[u] == '(' {
        k := u
        //fmt.Printf("k: %d, u: %d\n", k, u)
        for (k < len(s) && s[k] == '('){
            k++
        }
        //fmt.Printf("k: %d, u: %d\n", k, u)
        left -= k-u
        for i := k-u; i >= 0; i--{
            if left >= 0{
                dfs(s, k, path, cnt, left, right)    
            }
            path += string('(')
            //fmt.Printf("path: %s\n", path)
            cnt ++
            left ++
        }
    } else if s[u] == ')' {
        k := u
        for (k < len(s) && s[k] == ')'){
            k++
        }
        right -= k-u
        for i := k-u; i >= 0; i--{
            if right >= 0 && cnt >= 0 {
                dfs(s, k, path, cnt, left, right)
            }
            path += string(')')
            cnt --
            right ++
        }
    }

}
posted @ 2020-11-24 15:10  ganshuoos  阅读(129)  评论(0编辑  收藏  举报