"Remove Invalid Parenthesis Faster and Easier" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "Remove Invalid Parenthesis Faster and Easier" answered properly. Developers are finding an appropriate answer about Remove Invalid Parenthesis Faster and Easier related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like Remove Invalid Parenthesis Faster and Easier. Enter your desired code related query in the search bar and get every piece of information about C# code related question on Remove Invalid Parenthesis Faster and Easier.
Remove Invalid Parenthesis Faster and Easier
public List<String> removeInvalidParentheses(String s) {
Set<String> res = new HashSet<>();
int rmL = 0, rmR = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '(') rmL++;
if(s.charAt(i) == ')') {
if(rmL != 0) rmL--;
else rmR++;
}
}
DFS(res, s, 0, rmL, rmR, 0, new StringBuilder());
return new ArrayList<String>(res);
}
public void DFS(Set<String> res, String s, int i, int rmL, int rmR, int open, StringBuilder sb) {
if(i == s.length() && rmL == 0 && rmR == 0 && open == 0) {
res.add(sb.toString());
return;
}
if(i == s.length() || rmL < 0 || rmR < 0 || open < 0) return;
char c = s.charAt(i);
int len = sb.length();
if(c == '(') {
DFS(res, s, i + 1, rmL - 1, rmR, open, sb);
DFS(res, s, i + 1, rmL, rmR, open + 1, sb.append(c));
} else if(c == ')') {
DFS(res, s, i + 1, rmL, rmR - 1, open, sb);
DFS(res, s, i + 1, rmL, rmR, open - 1, sb.append(c));
} else {
DFS(res, s, i + 1, rmL, rmR, open, sb.append(c));
}
sb.setLength(len);
}
Source: kennyzhuang.gitbooks.io
All those coders who are working on the C# based application and are stuck on Remove Invalid Parenthesis Faster and Easier can get a collection of related answers to their query. Programmers need to enter their query on Remove Invalid Parenthesis Faster and Easier related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Remove Invalid Parenthesis Faster and Easier for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of Remove Invalid Parenthesis Faster and Easier while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "Remove Invalid Parenthesis Faster and Easier". Visit this developer's friendly online web community, CodeProZone, and get your queries like Remove Invalid Parenthesis Faster and Easier resolved professionally and stay updated to the latest C# updates.