Skip to content

Commit

Permalink
added GenerateParentheses.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed May 29, 2018
1 parent 7920d94 commit 0adbc0d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions company/google/GenerateParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class GenerateParentheses {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
generateParenthesisRecursive(result, "", 0, 0, n);

return result;
}

public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) {
if(current.length() == n * 2) {
result.add(current);
return;
}

if(open < n) {
generateParenthesisRecursive(result, current + "(", open + 1, close, n);
}

if(close < open) {
generateParenthesisRecursive(result, current + ")", open, close + 1, n);
}
}
}
23 changes: 23 additions & 0 deletions company/uber/GenerateParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class GenerateParentheses {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
generateParenthesisRecursive(result, "", 0, 0, n);

return result;
}

public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) {
if(current.length() == n * 2) {
result.add(current);
return;
}

if(open < n) {
generateParenthesisRecursive(result, current + "(", open + 1, close, n);
}

if(close < open) {
generateParenthesisRecursive(result, current + ")", open, close + 1, n);
}
}
}
23 changes: 23 additions & 0 deletions leetcode/backtracking/GenerateParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class GenerateParentheses {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
generateParenthesisRecursive(result, "", 0, 0, n);

return result;
}

public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) {
if(current.length() == n * 2) {
result.add(current);
return;
}

if(open < n) {
generateParenthesisRecursive(result, current + "(", open + 1, close, n);
}

if(close < open) {
generateParenthesisRecursive(result, current + ")", open, close + 1, n);
}
}
}
23 changes: 23 additions & 0 deletions leetcode/string/GenerateParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class GenerateParentheses {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
generateParenthesisRecursive(result, "", 0, 0, n);

return result;
}

public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) {
if(current.length() == n * 2) {
result.add(current);
return;
}

if(open < n) {
generateParenthesisRecursive(result, current + "(", open + 1, close, n);
}

if(close < open) {
generateParenthesisRecursive(result, current + ")", open, close + 1, n);
}
}
}

0 comments on commit 0adbc0d

Please sign in to comment.