Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Pattern;
- public class Solution {
- public static void main(String []argh)
- {
- Scanner sc = new Scanner(System.in);
- boolean flag= true; int count=0;String count1 = null;;
- while (sc.hasNext()) {
- String input=sc.next();
- //Complete the code
- if(input.length()==0)
- {
- flag= true;
- break;
- }
- else
- {
- for(int i=0;i<input.length();i++)
- {
- // System.out.println("alp" + i);
- for(int j=i+1;j<input.length();j++){
- if(input.charAt(i)=='{')
- {
- //System.out.println("a");
- if(input.charAt(j)=='}')
- {
- count=count+2;;
- break;
- }
- else
- {
- //System.out.println("b");
- count1="shrey";
- // System.out.println("b1");
- }
- }
- else if(input.charAt(i)=='[')
- {
- //System.out.println("a"+i);
- if(input.charAt(j)==']')
- {
- count=count+2;;
- break;
- }
- else
- {
- count1="shrey";
- // System.out.println("b2");
- }
- }
- else if(input.charAt(i)=='(')
- {
- if(input.charAt(j)==')')
- {
- count=count+2;;
- break;
- }
- else
- {
- count1="shrey";
- // System.out.println("b3" + count1);
- }
- }
- }
- }
- }
- if(count%2==0 && input.length()%2==0)
- {
- if(!(input.charAt(input.length()-1)=='{' || input.charAt(input.length()-1)=='[' || input.charAt(input.length()-1)=='(')){
- //System.out.println(input.charAt(input.length()-1));
- flag=true;
- if((Pattern.matches("[{]*",input)&&(!Pattern.matches("[}]*",input))))
- {
- flag= false;
- }
- else if((Pattern.matches("[}]*",input)&&(!Pattern.matches("[{]*",input))))
- {
- flag= false;
- }
- else if((Pattern.matches("[)]*",input)&&(!Pattern.matches("[(]*",input))))
- {
- flag= false;
- }
- else if((Pattern.matches("[(]*",input)&&(!Pattern.matches("[)]*",input))))
- {
- flag= false;
- }
- }
- else
- {
- //System.out.println("b");
- flag=false;
- }
- }
- else
- {
- flag= false;
- }
- System.out.println(flag);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement