简体   繁体   中英

Generate Parentheses in LeetCode

I'm working on solving problem Generate Parentheses on LeetCode . My first thought was that the n+1 th pair of parentheses could be deduced from n th pair of parentheses. Saying, if we use e as the situation of n th, and I thought the n+1 th will be one of the following situation:

  • ( + e + )
  • () + e
  • e + ()

And I came up with the following code.

public class Solution {
    public List<String> generateParenthesis(int n) {
        HashSet<String> temp = new HashSet<String>();
        HashSet<String> result = new HashSet<String>();
        List<String> rsult = new ArrayList<String>();

        if (n == 0) {
            temp.add("");
            return new ArrayList<String>(temp);
        }

        temp.add("()");

        if (n == 1) {

            return new ArrayList<String>(temp);
        }

        for (int i = 2; i <= n; i++) {
            result.removeAll(temp);
            for (String e : temp) {
                if (!result.contains("(" + e + ")")) {
                    result.add("(" + e + ")");
                }

                if (!result.contains("()" + e)) {
                    result.add("()" + e);
                }

                if (!result.contains(e + "()")) {
                    result.add(e + "()");
                }

            }

            temp.clear();
            temp.addAll(result);
        }
        rsult = new ArrayList<String>(result);
        Collections.sort(rsult);
        return rsult;
    }
}

However, when I submitted the code, I found that I still missed some cases when the n+1 is even. So I updated my code as below.

public class Solution {
    public List<String> generateParenthesis(int n) {
        HashSet<String> temp = new HashSet<String>();
        HashSet<String> result = new HashSet<String>();
        List<String> rsult = new ArrayList<String>();

        if (n == 0) {
            temp.add("");
            return new ArrayList<String>(temp);
        }

        temp.add("()");

        if (n == 1) {

            return new ArrayList<String>(temp);
        }

        for (int i = 2; i <= n; i++) {
            result.removeAll(temp);
            for (String e : temp) {
                if (!result.contains("(" + e + ")")) {
                    result.add("(" + e + ")");
                }

                if (!result.contains("()" + e)) {
                    result.add("()" + e);
                }

                if (!result.contains(e + "()")) {
                    result.add(e + "()");
                }

                if (i % 2 == 0) {
                    String dblprt = new String();
                    for(int j = 0; j< i/2;j++) {
                        dblprt = "(" + dblprt + ")";
                    }
                    dblprt = dblprt + dblprt ;
                    if (!result.contains(dblprt)) {
                        result.add(dblprt);
                    }
                }
            }

            temp.clear();
            temp.addAll(result);
        }
        rsult = new ArrayList<String>(result);
        Collections.sort(rsult);
        return rsult;
    }
}

Still, the test cases failed. So I'm confused. Why doesn't my way work? Am I still missing some cases?

Why doesn't my way work? Am I still missing some cases?

Consider the parentheses (())(()) , the only way for this to have come from your algorithm is if e = ())(() , and then ( + e + ) but in that case e is not a well-formed parentheses so e never existed and you have missed a case.

EDIT : it would seem that your second revision solves cases such as ()() , (())(()) , ((()))((())) but what about (()())(()()) , or (()())()(()) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM