Sort by

recency

|

645 Discussions

|

  • + 0 comments

    import java.util.*; class Solution{

    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
        List<String> input = new ArrayList<>();
        while (sc.hasNext()) {
            input.add(sc.next());
        }
    
              List<Stack<Character>> stacks = new ArrayList<>();
    
              for(String st : input)
              {    Stack<Character> characters = new Stack<>();
                   for (char ch : st.toCharArray()) 
                   {
                       characters.push(ch);
                   }
                   stacks.add(characters);
              }
    
           for(Stack stk : stacks)
           {
             int a = 0;
             int b = 0;
             int c = 0;
             for(int i = stk.size() -1; i > -1 ; i--) 
             {
              Character ch = (Character) stk.elementAt(i);   
              if((ch == '(' ))
               {   
                   stk.pop();
                   a++;
               }
               if((ch == ')' ))
               {   
                   stk.pop();
                   if(a == 1){
                    a = 3;
                   }
                   a--;
               }
                if((ch == '[' ))
               {   
                   stk.pop();
                   if(b == 1){
                    a = 3;
                   }
                   b++;
               }
               if((ch == ']' ))
               {   
                   stk.pop();
                   if(b == 1){
                    b = 3;
                   }
                   b--;
               }
               if((ch == '{' ))
               {   
                   stk.pop();
                   c++;
               }
               if((ch == '}' ))
               {   
                   stk.pop();
                   if(c == 1){
                    c = 3;
                   }
                   c--;
               }
        }
        System.out.println(((a+b+c) == 0)? true : false);
      }
    }
    

    }

  • + 1 comment

    class Solution{

    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
    
        while (sc.hasNext()) {
         String s = sc.nextLine();
         int n = s.length();
         int counter = 0;
         int counter1 = 0;
         if(s.startsWith("}") || s.startsWith("]") || s.startsWith(")")){
            System.out.println("false");
         }else if(s.endsWith("(") || s.endsWith("{") || s.endsWith("[")){
             System.out.println("false");
         }else {
    
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
                    counter++;
                }
                if (s.charAt(i) == ')' || s.charAt(i) == ']' || s.charAt(i) == '}') {
                    counter1++;
                }
            }
            if (counter == counter1) {
              System.out.println("true");
            } else {
              System.out.println("false");
            }
        }
     }
    

    } }

    • + 0 comments

      isnt this solution just wrong for this input {(}) ?

  • + 0 comments

    A solution without using stack -

    import java.util.*; class Solution{

    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
    
        while (sc.hasNext()) {
            String input=sc.next();
            int count = 0;
            //Complete the code
            if (input.length() % 2 == 0){
               for (int i = 0 ; i < input.length(); i++){
                    if (input.charAt(i) == '{' ||
                        input.charAt(i) == '[' ||
                        input.charAt(i) == '('){
                        count+=1;
                    }
                    else{
                        count-=1;
                    }
                    if(count < 0){
                        System.out.println("false");
                        break;
                    }
                } 
                if (count == 0){
                    System.out.println("true");
                }
            }
            else {
                System.out.println("false");
            }
    
        }
    
    }
    

    }

  • + 0 comments
    import java.util.Scanner;
    import java.util.Stack;
    
    public class Solution {
    
        private static final char KEY_OPENS_BRACKET = '{';
        private static final char KEY_CLOSES_BRACKET = '}';
        private static final char PARENTHESIS_OPENS_BRACKET = '(';
        private static final char PARENTHESIS_CLOSES_BRACKET = ')';
        private static final char SQUARE_PARENTHESIS_OPENS_BRACKET = '[';
        private static final char SQUARE_PARENTHESIS_CLOSES_BRACKET = ']';
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            while(scanner.hasNext()){
                Stack<Character> stack = new Stack<>();
                String input = scanner.nextLine();
                System.out.println(isValid(input, stack));
            }
            scanner.close();
        }
    
        private static boolean isValid(String input, Stack<Character> stack) {
            for(char chr : input.toCharArray()) {
                if(isOpenBracket(chr)) {
                    stack.push(chr);
                    continue;
                }
                if(chr == KEY_CLOSES_BRACKET  && !stack.isEmpty() && stack.peek() == KEY_OPENS_BRACKET) {
                    stack.pop();
                    continue;
                }
                if(chr == PARENTHESIS_CLOSES_BRACKET && !stack.isEmpty() && stack.peek() == PARENTHESIS_OPENS_BRACKET) {
                    stack.pop();
                    continue;
                }
                if (chr == SQUARE_PARENTHESIS_CLOSES_BRACKET &&!stack.isEmpty() && stack.peek() == SQUARE_PARENTHESIS_OPENS_BRACKET) {
                    stack.pop();
                    continue;
                }
                if (isCloseBracket(chr)) {
                    stack.push(chr);
                    break;
                }
            }
            return stack.isEmpty();
        }
    
        private static boolean isOpenBracket(char chr) {
            return switch (chr) {
                case KEY_OPENS_BRACKET, PARENTHESIS_OPENS_BRACKET, SQUARE_PARENTHESIS_OPENS_BRACKET -> true;
                default -> false;
            };
        }
    
        private static boolean isCloseBracket(char chr) {
            return switch (chr) {
                case KEY_CLOSES_BRACKET, PARENTHESIS_CLOSES_BRACKET, SQUARE_PARENTHESIS_CLOSES_BRACKET -> true;
                default -> false;
            };
        }
    }
    
  • + 0 comments

    Use helper method to check if balanced then return boolean if the stack is empty

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner input = new Scanner(System.in); 
            while(input.hasNext()) {
                String line = input.nextLine(); 
                Stack<Character> lineStak = new Stack<>(); 
                for (char chars : line.toCharArray()) {
                    if(lineStak.empty()) {
                        lineStak.push(chars); 
                    }
                    else if (balanced(lineStak.peek(), chars)) lineStak.pop(); 
                    else lineStak.push(chars);  
                }
                System.out.println(lineStak.empty());
                
            }
        }
        
        public static boolean balanced(char c1, char c2 ) {
            
            return 
                (c1 == '{' && c2 == '}') ||
                (c1 == '(' && c2 == ')') ||
                (c1 == '[' && c2 == ']')
            ;  
        }
    }