Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jaeinkim/be149598b48cb3431852473ada68e0dd to your computer and use it in GitHub Desktop.

Select an option

Save jaeinkim/be149598b48cb3431852473ada68e0dd to your computer and use it in GitHub Desktop.
import java.util.*;
public class Solution {
/***
*
*inputString result
* Hello, world! 0
* line [plus] 1
* if (Count of eggs is 4.) {Buy milk.} 2
* >_< -1
*
* open_cnt < closed_cnt이면, -1 반환
*
*/
public static int open_cnt = 0;
public static int close_cnt = 0;
public static int[] num_arr;
public static int answer;
public static void main(String argv[]){
Solution sol = new Solution();
System.out.println(sol.solution("if (Count of eggs is 4.) {Buy milk.}"));
}
public int solution(String inputString){
char[] arr = inputString.toCharArray();
num_arr = new int[4];
for (char ch : arr){
check(ch, '(', ')', 0);
if(answer == -1){return answer;}
}
open_cnt = 0;
close_cnt = 0;
for (char ch : arr){
check(ch, '{', '}', 1);
if(answer == -1){return answer;}
}
open_cnt = 0;
close_cnt = 0;
for (char ch : arr){
check(ch, '[', ']', 2);
if(answer == -1){return answer;}
}
open_cnt = 0;
close_cnt = 0;
for (char ch : arr){
check(ch, '<', '>', 3);
if(answer == -1){return answer;}
}
open_cnt = 0;
close_cnt = 0;
for(int i =0; i <num_arr.length; i++){
answer += num_arr[i]/2;
}
return answer;
}
public void check(char ch, char open_cpr, char close_cpr, int idx){
if(open_cnt < close_cnt ){
answer = -1;
}else{
if(ch == open_cpr){
num_arr[idx]++;
open_cnt++;
}else if (ch == close_cpr){
num_arr[idx]++;
close_cnt++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment