zoukankan      html  css  js  c++  java
  • 1249. Minimum Remove to Make Valid Parentheses (Solution 2: Two ArrayList)

    package LeetCode_1249
    
    /**
     * 1249. Minimum Remove to Make Valid Parentheses
     * https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/description/
     *
     * Given a string s of '(' , ')' and lowercase English characters.
    Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.
    Formally, a parentheses string is valid if and only if:
    1. It is the empty string, contains only lowercase characters, or
    2. It can be written as AB (A concatenated with B), where A and B are valid strings, or
    3. It can be written as (A), where A is a valid string.
    
    Example 1:
    Input: s = "lee(t(c)o)de)"
    Output: "lee(t(c)o)de"
    Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
    
    Example 2:
    Input: s = "a)b(c)d"
    Output: "ab(c)d"
    
    Example 3:
    Input: s = "))(("
    Output: ""
    Explanation: An empty string is also valid.
    
    Example 4:
    Input: s = "(a(b(c)d)"
    Output: "a(b(c)d)"
    
    Constraints:
    1 <= s.length <= 10^5
    s[i] is one of  '(' , ')' and lowercase English letters.
     * */
    class Solution {
        /*
        * solution 1: BFS, TLE, Time complexity:O(2^n), Space complexity:O(2^n);
        *
        * solution 2: two list, keep tracking the index of open and close parentheses, 
        * Time complexity:O(n), Space complexity:O(n);
        * */
        fun minRemoveToMakeValid(s: String): String {
            //solution 2:
            val open = ArrayList<Int>()
            val close = ArrayList<Int>()
            for (i in s.indices) {
                val c = s[i]
                if (c == '(') {
                    open.add(i)
                } else if (c == ')') {
                    if (open.isNotEmpty()) {
                        //for keep both balance
                        open.removeAt(open.lastIndex)
                    } else {
                        close.add(i)
                    }
                }
            }
            val charArray = s.toCharArray()
            //replace the invalid parentheses
            for (i in open) {
                charArray[i] = '#'
            }
            for (i in close) {
                charArray[i] = '#'
            }
            val result = StringBuilder()
            for (ch in charArray) {
                if (ch != '#') {
                    result.append(ch)
                }
            }
            return result.toString()
        }
    }
  • 相关阅读:
    Maven 集成Tomcat插件
    dubbo 序列化 问题 属性值 丢失 ArrayList 解决
    docker 中安装 FastDFS 总结
    docker 从容器中拷文件到宿主机器中
    db2 相关命令
    Webphere WAS 启动
    CKEDITOR 4.6.X 版本 插件 弹出对话框 Dialog中 表格 Table 自定义样式Style 问题
    SpringMVC JSONP JSON支持
    CKEDITOR 3.4.2中 按钮事件中 动态改变图标和title 获取按钮
    git回退到远程某个版本
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13423898.html
Copyright © 2011-2022 走看看