Repository navigation
Expand file tree
/
Copy pathline01.java
More file actions
89 lines (69 loc) · 1.78 KB
/
Copy pathline01.java
File metadata and controls
89 lines (69 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Test;
import java.util.Stack;
public class line01 {
public static int solution(String inputString) {
int answer = 0;
Stack<Character> st = new Stack<>();
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) == '(') { // (들어오면 입력
st.add('(');
} else if (inputString.charAt(i) == '[') {// [ 들어오면 입력
st.add('[');
} else if (inputString.charAt(i) == '{') {// [ 들어오면 입력
st.add('{');
} else if (inputString.charAt(i) == '<') {// [ 들어오면 입력
st.add('<');
}
else if (inputString.charAt(i) == ']') {
if (!st.isEmpty() && st.contains('[')) { //마지막게 아니고 포함 !!!!!
int idx = st.indexOf('['); // stack 의 인덱스를 가져와서 제거
st.remove(idx);
answer++;
} else {
st.add(']');
break;
}
} else if (inputString.charAt(i) == ')') {
if (!st.isEmpty() && st.contains('(')) {
int idx = st.indexOf('(');
st.remove(idx);
answer++;
} else {
st.add(')');
break;
}
} else if (inputString.charAt(i) == '}') {
if (!st.isEmpty() && st.contains('{')) {
int idx = st.indexOf('{');
st.remove(idx);
answer++;
} else {
st.add('}');
break;
}
} else if (inputString.charAt(i) == '>') {
if (!st.isEmpty() && st.contains('<')) {
int idx = st.indexOf('<');
st.remove(idx);
answer++;
} else {
st.add('>');
break;
}
}
}
if (!st.isEmpty()) {
answer = -1;
}
System.out.println(answer);
return answer;
}
public static void main(String[] args) {
String s = "Hello, world!";
String s1 = "line [plus]";
String s2 = "if (Count of eggs is 4.) {Buy milk.}";
String s3 = ">_<";
String s4 = "{(})";
solution(s4);
}
}