From ddaa6a1a5af76cc919b22119ff05c01da903c152 Mon Sep 17 00:00:00 2001 From: Ryo Oshima Date: Sat, 20 Apr 2024 13:28:14 +0900 Subject: [PATCH 1/5] 22. Generate Parentheses --- 22. Generate Parentheses/solution.java | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 22. Generate Parentheses/solution.java diff --git a/22. Generate Parentheses/solution.java b/22. Generate Parentheses/solution.java new file mode 100644 index 0000000..f85c54c --- /dev/null +++ b/22. Generate Parentheses/solution.java @@ -0,0 +1,31 @@ +class Solution { + // 再帰 + // ACだが、途中でメモ化をしてないのでかなり計算量が悪くなってTLEになった + public List generateParenthesis(int n) { + Set resultParenetheses = new HashSet<>(); + Set pastState = new HashSet<>(); + insertParenetheses(n, new StringBuilder(), resultParenetheses, pastState); + return new ArrayList<>(resultParenetheses); + } + + + private void insertParenetheses(int n, StringBuilder current, Set resultParenetheses, Set pastState) { + if (n == 0) { + resultParenetheses.add(new String(current)); + return; + } + if (pastState.contains(new String(current))) { + return; + } + pastState.add(new String(current)); + for(int i = 0; i <= current.length(); i++) { + current.insert(i, "("); + for(int j = i + 1; j <= current.length(); j++) { + current.insert(j, ")"); + insertParenetheses(n - 1, current, resultParenetheses, pastState); + current.delete(j, j + 1); + } + current.delete(i, i + 1); + } + } +} \ No newline at end of file From 5011edf4246f958f496261c6d5b612ab562ba787 Mon Sep 17 00:00:00 2001 From: Ryo Oshima Date: Sun, 21 Apr 2024 14:16:55 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=81=84=E3=81=9F=E3=81=A0=E3=81=84=E3=81=9F=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E5=9B=9E=E7=AD=94=E8=BF=BD=E5=8A=A0=E3=80=81?= =?UTF-8?q?=E3=81=8A=E3=82=88=E3=81=B3=E3=82=A2=E3=83=97=E3=83=AD=E3=83=BC?= =?UTF-8?q?=E3=83=81=E6=96=B9=E6=B3=95=E3=81=AA=E3=81=A9=E3=81=8AREADME?= =?UTF-8?q?=E3=81=AB=E8=A8=98=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 22. Generate Parentheses/README.md | 13 ++++++ 22. Generate Parentheses/solution2.java | 56 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 22. Generate Parentheses/README.md create mode 100644 22. Generate Parentheses/solution2.java diff --git a/22. Generate Parentheses/README.md b/22. Generate Parentheses/README.md new file mode 100644 index 0000000..9de6cfc --- /dev/null +++ b/22. Generate Parentheses/README.md @@ -0,0 +1,13 @@ +# 1 回目 + +アプローチとしては()を再帰的に適切な位置に挿入していき、最終的な長さになったら HashSet に加えていくというもの(重複を防ぐために Set を使っています)。 +適切な位置というのは、挿入した"("に対して、"("の位置より右側で")"が挿入されることをいみしています。(これで正しい文字列であることは保証されるので) +途中でメモ化していないことによる TLE になったので、途中の文字列についても保持して重複した再帰処理を防ぐようにしました・ + +# 2 回目 + +コメントいただいた方法で修正しました。 +アプローチとしては、末尾に"(",")"を挿入して Queue に保存していき、最終的に文字列の長さが 2 _ n になったら、正しい文字列かどうかを確認して正しければ List に加えるというものです。 +途中で正しい文字列か(")"のが多くならないか)をチェックしています。こちらについては最後だけ正しい文字列かをチェックする方法もあり、処理のシンプルさでは最後だけチェックのがよいですが、速度的に n が大きくなればなるほど最終的にチェックする数が増えると思うので、途中で判定して Queue に追加するものを選択したほうがはやくなると思っています。 +時間計算量がわかりやすくて、文字列の長さ分毎回2倍増えていきますので、2^(2n)かかり、あと正しいかの判定に 2n かかるので、O(2^(2n) _ n)となるかと思います。 +空間計算量についても、Queue に入る最大の要素数は 2^(2n - 1), 要素となる文字列の長さは最大 2n なので O(2^(2n) \* n)となるかとおもいます。 diff --git a/22. Generate Parentheses/solution2.java b/22. Generate Parentheses/solution2.java new file mode 100644 index 0000000..423921d --- /dev/null +++ b/22. Generate Parentheses/solution2.java @@ -0,0 +1,56 @@ +class Solution { + public List generateParenthesis(int n) { + List parenthesisStrings = new ArrayList<>(); + Queue parenthesisCandidate = new LinkedList<>(); + parenthesisCandidate.add(""); + + while (!parenthesisCandidate.isEmpty()) { + String current = parenthesisCandidate.poll(); + if (!isValidParenethesisInProcess(current)) { + continue; + } + if (current.length() == 2 * n) { + if (isValidParenethesis(current)) { + parenthesisStrings.add(current); + } + continue; + } + parenthesisCandidate.add(current + "("); + parenthesisCandidate.add(current + ")"); + } + return parenthesisStrings; + } + + private boolean isValidParenethesisInProcess(String string) { + int leftParenethesisCount = 0; + for (char c : string.toCharArray()) { + if (c == '(') { + leftParenethesisCount++; + } else { + leftParenethesisCount--; + } + + if (leftParenethesisCount < 0) { + return false; + } + } + return leftParenethesisCount >= 0; + } + + private boolean isValidParenethesis(String string) { + int leftParenethesisCount = 0; + for (char c : string.toCharArray()) { + if (c == '(') { + leftParenethesisCount++; + } else { + leftParenethesisCount--; + } + + if (leftParenethesisCount < 0) { + return false; + } + } + return leftParenethesisCount == 0; + } + +} From 1b3da057171c9bfb7d2fc378a24775509656f1e2 Mon Sep 17 00:00:00 2001 From: Ryo Oshima Date: Sun, 21 Apr 2024 17:15:31 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=E6=8C=87=E6=91=98=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{solution.java => firstSolution.java} | 9 +-- .../{README.md => note.md} | 19 ++++++- ...2.java => refactoredByMyselfSolution.java} | 0 .../refactoredSolution.java | 56 +++++++++++++++++++ .../refactoredSolution1.java | 29 ++++++++++ .../refactoredSolution2.java | 27 +++++++++ 6 files changed, 133 insertions(+), 7 deletions(-) rename 22. Generate Parentheses/{solution.java => firstSolution.java} (84%) rename 22. Generate Parentheses/{README.md => note.md} (56%) rename 22. Generate Parentheses/{solution2.java => refactoredByMyselfSolution.java} (100%) create mode 100644 22. Generate Parentheses/refactoredSolution.java create mode 100644 22. Generate Parentheses/refactoredSolution1.java create mode 100644 22. Generate Parentheses/refactoredSolution2.java diff --git a/22. Generate Parentheses/solution.java b/22. Generate Parentheses/firstSolution.java similarity index 84% rename from 22. Generate Parentheses/solution.java rename to 22. Generate Parentheses/firstSolution.java index f85c54c..4145251 100644 --- a/22. Generate Parentheses/solution.java +++ b/22. Generate Parentheses/firstSolution.java @@ -9,7 +9,8 @@ public List generateParenthesis(int n) { } - private void insertParenetheses(int n, StringBuilder current, Set resultParenetheses, Set pastState) { + private void insertParenetheses(int n, StringBuilder current, Set resultParenetheses, + Set pastState) { if (n == 0) { resultParenetheses.add(new String(current)); return; @@ -18,9 +19,9 @@ private void insertParenetheses(int n, StringBuilder current, Set result return; } pastState.add(new String(current)); - for(int i = 0; i <= current.length(); i++) { + for (int i = 0; i <= current.length(); i++) { current.insert(i, "("); - for(int j = i + 1; j <= current.length(); j++) { + for (int j = i + 1; j <= current.length(); j++) { current.insert(j, ")"); insertParenetheses(n - 1, current, resultParenetheses, pastState); current.delete(j, j + 1); @@ -28,4 +29,4 @@ private void insertParenetheses(int n, StringBuilder current, Set result current.delete(i, i + 1); } } -} \ No newline at end of file +} diff --git a/22. Generate Parentheses/README.md b/22. Generate Parentheses/note.md similarity index 56% rename from 22. Generate Parentheses/README.md rename to 22. Generate Parentheses/note.md index 9de6cfc..3582eb1 100644 --- a/22. Generate Parentheses/README.md +++ b/22. Generate Parentheses/note.md @@ -1,13 +1,26 @@ -# 1 回目 +# firstSolution アプローチとしては()を再帰的に適切な位置に挿入していき、最終的な長さになったら HashSet に加えていくというもの(重複を防ぐために Set を使っています)。 適切な位置というのは、挿入した"("に対して、"("の位置より右側で")"が挿入されることをいみしています。(これで正しい文字列であることは保証されるので) 途中でメモ化していないことによる TLE になったので、途中の文字列についても保持して重複した再帰処理を防ぐようにしました・ -# 2 回目 +# refactoredByMyselfSolution -コメントいただいた方法で修正しました。 +# refactoredSolution + +0, 変数名修正 +コメントいただいた箇所について修正しました。 +状態についても必要十分で変数名に含めてあげる意識が足りないなと思いました。 + +1, Queue を使った loop +コメントいただいた方法を元に以下のように考えました。 アプローチとしては、末尾に"(",")"を挿入して Queue に保存していき、最終的に文字列の長さが 2 _ n になったら、正しい文字列かどうかを確認して正しければ List に加えるというものです。 途中で正しい文字列か(")"のが多くならないか)をチェックしています。こちらについては最後だけ正しい文字列かをチェックする方法もあり、処理のシンプルさでは最後だけチェックのがよいですが、速度的に n が大きくなればなるほど最終的にチェックする数が増えると思うので、途中で判定して Queue に追加するものを選択したほうがはやくなると思っています。 +途中でコメントもいただき、左の数を文字列に対して保持しておけば、毎回チェックの処理を O(n)で走らせないですむので、新規に(の数を保持しておく Queue を作りました。 時間計算量がわかりやすくて、文字列の長さ分毎回2倍増えていきますので、2^(2n)かかり、あと正しいかの判定に 2n かかるので、O(2^(2n) _ n)となるかと思います。 空間計算量についても、Queue に入る最大の要素数は 2^(2n - 1), 要素となる文字列の長さは最大 2n なので O(2^(2n) \* n)となるかとおもいます。 + +2, 再帰を使った方法 +再帰については、基本的に上記と考え方が同じなので、すんなり実装はできました。コメントいただいた通り、途中で条件をしっかり確認すればかなり不要な文字列のチェックを抑えることができるので、しっかり実装前や途中で不要なパターンをしっかり明確化できるのかが大事だと改めて感じました。 +また String の add に文字数分のコストがかかることを教えていただき、かなり処理時間に差が出るので StringBuilder を使ったアプローチを常に考えようと思いました。 +[確認した他の実装](https://github.com/hayashi-ay/leetcode/pull/70) diff --git a/22. Generate Parentheses/solution2.java b/22. Generate Parentheses/refactoredByMyselfSolution.java similarity index 100% rename from 22. Generate Parentheses/solution2.java rename to 22. Generate Parentheses/refactoredByMyselfSolution.java diff --git a/22. Generate Parentheses/refactoredSolution.java b/22. Generate Parentheses/refactoredSolution.java new file mode 100644 index 0000000..04b0c23 --- /dev/null +++ b/22. Generate Parentheses/refactoredSolution.java @@ -0,0 +1,56 @@ +class Solution { + public List generateParenthesis(int n) { + List allParentheses = new ArrayList<>(); + Queue parenthesisCandidate = new LinkedList<>(); + parenthesisCandidate.add(""); + + while (!parenthesisCandidate.isEmpty()) { + String current = parenthesisCandidate.poll(); + if (!isValidParenethesisInProcess(current)) { + continue; + } + if (current.length() == 2 * n) { + if (isValidParenethesis(current)) { + parenthesisStrings.add(current); + } + continue; + } + parenthesisCandidate.add(current + "("); + parenthesisCandidate.add(current + ")"); + } + return parenthesisStrings; + } + + private boolean isValidParenethesisInProcess(String string) { + int notClosedleftParenethesisCount = 0; + for (char c : string.toCharArray()) { + if (c == '(') { + notClosedleftParenethesisCount++; + } else { + notClosedleftParenethesisCount--; + } + + if (notClosedleftParenethesisCount < 0) { + return false; + } + } + return notClosedleftParenethesisCount >= 0; + } + + private boolean isValidParenethesis(String string) { + int notClosedleftParenethesisCount = 0; + for (char c : string.toCharArray()) { + if (c == '(') { + notClosedleftParenethesisCount++; + } else { + notClosedleftParenethesisCount--; + } + + if (notClosedleftParenethesisCount < 0) { + return false; + } + } + return notClosedleftParenethesisCount == 0; + } + +} diff --git a/22. Generate Parentheses/refactoredSolution1.java b/22. Generate Parentheses/refactoredSolution1.java new file mode 100644 index 0000000..965677b --- /dev/null +++ b/22. Generate Parentheses/refactoredSolution1.java @@ -0,0 +1,29 @@ +class Solution { + public List generateParenthesis(int n) { + List allParentheses = new ArrayList<>(); + Queue parenthesisCandidate = new LinkedList<>(); + Queue unClosedleftParentheses = new LinkedList<>(); + parenthesisCandidate.add(""); + unClosedleftParentheses.add(0); + + while (!parenthesisCandidate.isEmpty()) { + String current = parenthesisCandidate.poll(); + int currentLeftParenthesisCount = unClosedleftParentheses.poll(); + + if (current.length() == 2 * n) { + allParentheses.add(current); + continue; + } + if (currentLeftParenthesisCount < 2 * n - current.length()) { + parenthesisCandidate.add(current + "("); + unClosedleftParentheses.add(currentLeftParenthesisCount + 1); + } + if (0 < currentLeftParenthesisCount) { + parenthesisCandidate.add(current + ")"); + unClosedleftParentheses.add(currentLeftParenthesisCount - 1); + } + } + return allParentheses; + } + +} diff --git a/22. Generate Parentheses/refactoredSolution2.java b/22. Generate Parentheses/refactoredSolution2.java new file mode 100644 index 0000000..b7d3e9c --- /dev/null +++ b/22. Generate Parentheses/refactoredSolution2.java @@ -0,0 +1,27 @@ +class Solution { + public List generateParenthesis(int n) { + List allParentheses = new ArrayList<>(); + makeCombination(n, allParentheses, new StringBuilder(), 0); + return allParentheses; + } + + private void makeCombination(int n, List allParentheses, StringBuilder current, + int unclosedLeftParentheses) { + if (current.length() == 2 * n) { + allParentheses.add(current.toString()); + return; + } + // 残りの挿入可能文字数が、閉じていない(の数より多い時は(を挿入 + if (unclosedLeftParentheses < 2 * n - current.length()) { + current.append("("); + makeCombination(n, allParentheses, current, unclosedLeftParentheses + 1); + current.deleteCharAt(current.length() - 1); + } + // 閉じていない(があるので)を挿入 + if (0 < unclosedLeftParentheses) { + current.append(")"); + makeCombination(n, allParentheses, current, unclosedLeftParentheses - 1); + current.deleteCharAt(current.length() - 1); + } + } +} From 8c9c8162126a09cb8e6600e11643983ae5903701 Mon Sep 17 00:00:00 2001 From: Ryo Oshima Date: Sun, 21 Apr 2024 17:43:39 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=E3=83=AA=E3=83=8D=E3=83=BC=E3=83=A0?= =?UTF-8?q?=E3=80=81=E3=82=AF=E3=83=A9=E3=82=B9=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 22. Generate Parentheses/note.md | 3 ++ .../refactoredSolution.java | 20 +++++------ .../refactoredSolution1.java | 36 +++++++++++-------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/22. Generate Parentheses/note.md b/22. Generate Parentheses/note.md index 3582eb1..476ff72 100644 --- a/22. Generate Parentheses/note.md +++ b/22. Generate Parentheses/note.md @@ -20,6 +20,9 @@ 時間計算量がわかりやすくて、文字列の長さ分毎回2倍増えていきますので、2^(2n)かかり、あと正しいかの判定に 2n かかるので、O(2^(2n) _ n)となるかと思います。 空間計算量についても、Queue に入る最大の要素数は 2^(2n - 1), 要素となる文字列の長さは最大 2n なので O(2^(2n) \* n)となるかとおもいます。 +追加いただいたコメントで、Queue は1つにまとました(管理の面でも、複数管理の場合は更新し忘れなどのリスクもあるのでまとめて管理したほうが合理的だと思いました。) +文字列と開いた(の数をまとめたクラスの命名については少し不安です(State とか Info とかしか浮かびませんでした)。 + 2, 再帰を使った方法 再帰については、基本的に上記と考え方が同じなので、すんなり実装はできました。コメントいただいた通り、途中で条件をしっかり確認すればかなり不要な文字列のチェックを抑えることができるので、しっかり実装前や途中で不要なパターンをしっかり明確化できるのかが大事だと改めて感じました。 また String の add に文字数分のコストがかかることを教えていただき、かなり処理時間に差が出るので StringBuilder を使ったアプローチを常に考えようと思いました。 diff --git a/22. Generate Parentheses/refactoredSolution.java b/22. Generate Parentheses/refactoredSolution.java index 04b0c23..b8a30cb 100644 --- a/22. Generate Parentheses/refactoredSolution.java +++ b/22. Generate Parentheses/refactoredSolution.java @@ -22,35 +22,35 @@ public List generateParenthesis(int n) { } private boolean isValidParenethesisInProcess(String string) { - int notClosedleftParenethesisCount = 0; + int notClosedLeftParenethesisCount = 0; for (char c : string.toCharArray()) { if (c == '(') { - notClosedleftParenethesisCount++; + notClosedLeftParenethesisCount++; } else { - notClosedleftParenethesisCount--; + notClosedLeftParenethesisCount--; } - if (notClosedleftParenethesisCount < 0) { + if (notClosedLeftParenethesisCount < 0) { return false; } } - return notClosedleftParenethesisCount >= 0; + return notClosedLeftParenethesisCount >= 0; } private boolean isValidParenethesis(String string) { - int notClosedleftParenethesisCount = 0; + int notClosedLeftParenethesisCount = 0; for (char c : string.toCharArray()) { if (c == '(') { - notClosedleftParenethesisCount++; + notClosedLeftParenethesisCount++; } else { - notClosedleftParenethesisCount--; + notClosedLeftParenethesisCount--; } - if (notClosedleftParenethesisCount < 0) { + if (notClosedLeftParenethesisCount < 0) { return false; } } - return notClosedleftParenethesisCount == 0; + return notClosedLeftParenethesisCount == 0; } } diff --git a/22. Generate Parentheses/refactoredSolution1.java b/22. Generate Parentheses/refactoredSolution1.java index 965677b..7058618 100644 --- a/22. Generate Parentheses/refactoredSolution1.java +++ b/22. Generate Parentheses/refactoredSolution1.java @@ -1,29 +1,37 @@ class Solution { public List generateParenthesis(int n) { List allParentheses = new ArrayList<>(); - Queue parenthesisCandidate = new LinkedList<>(); - Queue unClosedleftParentheses = new LinkedList<>(); - parenthesisCandidate.add(""); - unClosedleftParentheses.add(0); + Queue parenthesisCandidate = new LinkedList<>(); + parenthesisCandidate.add(new ParenthesisState("", 0)); while (!parenthesisCandidate.isEmpty()) { - String current = parenthesisCandidate.poll(); - int currentLeftParenthesisCount = unClosedleftParentheses.poll(); + ParenthesisState currentState = parenthesisCandidate.poll(); + String curerntString = currentState.str; + int numCurrentUnClosedLeftParenthesis = currentState.numUnClosedLeftParenthesis; - if (current.length() == 2 * n) { - allParentheses.add(current); + if (curerntString.length() == 2 * n) { + allParentheses.add(currentState.str); continue; } - if (currentLeftParenthesisCount < 2 * n - current.length()) { - parenthesisCandidate.add(current + "("); - unClosedleftParentheses.add(currentLeftParenthesisCount + 1); + if (numCurrentUnClosedLeftParenthesis < 2 * n - curerntString.length()) { + parenthesisCandidate.add(new ParenthesisState(curerntString + "(", + numCurrentUnClosedLeftParenthesis + 1)); } - if (0 < currentLeftParenthesisCount) { - parenthesisCandidate.add(current + ")"); - unClosedleftParentheses.add(currentLeftParenthesisCount - 1); + if (0 < numCurrentUnClosedLeftParenthesis) { + parenthesisCandidate.add(new ParenthesisState(curerntString + ")", + numCurrentUnClosedLeftParenthesis - 1)); } } return allParentheses; } + class ParenthesisState { + String str; + int numUnClosedLeftParenthesis; + + ParenthesisState(String str, int numUnClosedLeftParenthesis) { + this.str = str; + this.numUnClosedLeftParenthesis = numUnClosedLeftParenthesis; + } + } } From 86fe52163670051e5820b02f298e0f8f809edc3a Mon Sep 17 00:00:00 2001 From: Ryo Oshima Date: Sun, 21 Apr 2024 23:24:12 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=E3=83=AA=E3=83=8D=E3=83=BC=E3=83=A0?= =?UTF-8?q?=E3=80=81=E5=88=A5=E3=82=A2=E3=83=97=E3=83=AD=E3=83=BC=E3=83=81?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 22. Generate Parentheses/note.md | 6 ++++ .../refactoredSolution1.java | 8 +++--- .../refactoredSolution3.java | 28 +++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 22. Generate Parentheses/refactoredSolution3.java diff --git a/22. Generate Parentheses/note.md b/22. Generate Parentheses/note.md index 476ff72..99be5fa 100644 --- a/22. Generate Parentheses/note.md +++ b/22. Generate Parentheses/note.md @@ -27,3 +27,9 @@ 再帰については、基本的に上記と考え方が同じなので、すんなり実装はできました。コメントいただいた通り、途中で条件をしっかり確認すればかなり不要な文字列のチェックを抑えることができるので、しっかり実装前や途中で不要なパターンをしっかり明確化できるのかが大事だと改めて感じました。 また String の add に文字数分のコストがかかることを教えていただき、かなり処理時間に差が出るので StringBuilder を使ったアプローチを常に考えようと思いました。 [確認した他の実装](https://github.com/hayashi-ay/leetcode/pull/70) + +3, Queue を使った loop の別アプローチ +複数のアプローチを思い浮かべられるとよい+ループの考えについて教えていただいたので、それをベースに違うアプローチで()を挿入した。 +挿入については"(" + ")"\*n n:0 <= n <= 閉じてない(の個数 として挿入をしていく +挿入したものは被りはない +挿入中に n = 4 で((((((((( みたいにケースでは閉じる括弧を加えると溢れてしまうケースがありえるので、それを途中で阻止するようにした。 diff --git a/22. Generate Parentheses/refactoredSolution1.java b/22. Generate Parentheses/refactoredSolution1.java index 7058618..5927d04 100644 --- a/22. Generate Parentheses/refactoredSolution1.java +++ b/22. Generate Parentheses/refactoredSolution1.java @@ -7,7 +7,7 @@ public List generateParenthesis(int n) { while (!parenthesisCandidate.isEmpty()) { ParenthesisState currentState = parenthesisCandidate.poll(); String curerntString = currentState.str; - int numCurrentUnClosedLeftParenthesis = currentState.numUnClosedLeftParenthesis; + int numCurrentUnClosedLeftParenthesis = currentState.numUnclosedLeftParenthesis; if (curerntString.length() == 2 * n) { allParentheses.add(currentState.str); @@ -27,11 +27,11 @@ public List generateParenthesis(int n) { class ParenthesisState { String str; - int numUnClosedLeftParenthesis; + int numUnclosedLeftParenthesis; - ParenthesisState(String str, int numUnClosedLeftParenthesis) { + ParenthesisState(String str, int numUnclosedLeftParenthesis) { this.str = str; - this.numUnClosedLeftParenthesis = numUnClosedLeftParenthesis; + this.numUnclosedLeftParenthesis = numUnclosedLeftParenthesis; } } } diff --git a/22. Generate Parentheses/refactoredSolution3.java b/22. Generate Parentheses/refactoredSolution3.java new file mode 100644 index 0000000..913a575 --- /dev/null +++ b/22. Generate Parentheses/refactoredSolution3.java @@ -0,0 +1,28 @@ +class Solution { + public List generateParenthesis(int n) { + List allParentheses = new ArrayList<>(); + Queue> parenthesisCandidate = new LinkedList<>(); + parenthesisCandidate.add(new Pair<>("", 0)); + + while (!parenthesisCandidate.isEmpty()) { + Pair currentState = parenthesisCandidate.poll(); + String curerntString = currentState.getKey(); + int numCurrentUnClosedLeftParenthesis = currentState.getValue(); + + if (curerntString.length() == 2 * n && numCurrentUnClosedLeftParenthesis == 0) { + allParentheses.add(currentState.getKey()); + continue; + } + for (int i = 0; i <= numCurrentUnClosedLeftParenthesis + 1; i++) { + if (2 * n < curerntString.length() + 1 + i) { + continue; + } + String closeParenthesis = ")".repeat(i); + String nextCandidate = curerntString + "(" + closeParenthesis; + parenthesisCandidate.add(new Pair(nextCandidate, + numCurrentUnClosedLeftParenthesis + 1 - i)); + } + } + return allParentheses; + } +}