-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelimiters.java
More file actions
61 lines (52 loc) · 1.35 KB
/
Delimiters.java
File metadata and controls
61 lines (52 loc) · 1.35 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
import java.util.ArrayList;
/**
* @author Sofie Budman
* Period 5
*
*/
public class Delimiters {
private String openDel;
private String closeDel;
public Delimiters(String open, String close){
openDel = open;
closeDel = close;
}
public ArrayList<String> getDelimetersList(String[] tokens){
ArrayList<String> out = new ArrayList<String>();
for(String t: tokens){
if(t.equals(openDel) || t.equals(closeDel)){
out.add(t);
}
}
return out;
}
public boolean isBalanced (ArrayList<String> delimiters){
int o = 0;
int c = 0;
for(String s: delimiters){
if(s.equals(openDel)){
o ++;
} else if (s.equals(closeDel)){
c ++;
}
if(c > o){
return false;
}
}
return (o == c);
}
public static void main(String[] args){
Delimiters d = new Delimiters("<sup>", "</sup>");
String[] t = {"<q>", "yy", "</q>", "zz", "</q>"};
ArrayList<String> a = new ArrayList<String>();
a.add("<sup>");
a.add("</sup>");
a.add("</sup>");
/*
a.add("<sup>");
a.add("</sup>");
a.add("</sup>");*/
;
System.out.println(d.isBalanced(a));
}
}