Repository navigation
Expand file tree
/
Copy pathValidateHooks.java
More file actions
71 lines (58 loc) · 1.78 KB
/
Copy pathValidateHooks.java
File metadata and controls
71 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
public class ValidateHooks {
public static Set<String> validate(String input) {
Set<String> ans = new HashSet<String>();
if (input == null || input.length() == 0) {
ans.add("");
return ans;
}
Queue<String> queue = new LinkedList<String>();
queue.add(input);
boolean flag = false;
Set<String> tested = new HashSet<String>();
while (!queue.isEmpty()) {
int level = queue.size();
for (int i = 0; i < level; i++) {
String top = queue.poll();
if (tested.contains(top)) {
continue;
}
if (sequenceBracket(top)) {
flag = true;
ans.add(top);
}
if (!flag) {
tested.add(top);
for (int j = 0; j < top.length(); j++) {
String add = top.substring(0, j) + top.substring(j + 1, top.length());
queue.add(add);
}
}
}
if (flag) break;
}
return ans;
}
private static boolean sequenceBracket(String input) {
int balanceIndex = 0;
for (int i = 0; i < input.length(); i++) {
char current = input.charAt(i);
if (current == '{') {
balanceIndex++;
}
if (current == '}') {
if (balanceIndex == 0) {
return false;
}
balanceIndex--;
}
}
if (balanceIndex == 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
}
}