Sort by

recency

|

639 Discussions

|

  • + 0 comments

    Using Deque and Map (Java 15):

    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.Map;
    import java.util.Scanner;
    
    class Solution {
    
        private static final Map<Character, Character> brackets = Map.of(
            ')', '(',
            '}', '{',
            ']', '['
        );
        
        private static boolean check(String input) {
            Deque<Character> stack = new LinkedList<>();
    
            for (char c : input.toCharArray()) {
                if (brackets.containsKey(c)) {
                    if (stack.isEmpty()) return false;
    
                    if (brackets.get(c) != stack.removeLast()) return false;
    
                    continue;
                }
    
                stack.add(c);
            }
    
            return stack.isEmpty();
        }
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            while (sc.hasNext()) {
                boolean isBalanced = check(sc.next());
    
                System.out.println(isBalanced ? "true" : "false");
            }
    
            sc.close();
        }
    }
    
  • + 1 comment

    import java.util.*; class Solution{

    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
    
        while (sc.hasNext()) {
            String input=sc.next();
            //Complete the code
    
            Stack <Character> s = new Stack<>();
            for(int i = 0 ; i<input.length(); i++)
            {
                char  c = input.charAt(i);
    
                if(c=='{' || c=='(' ||  c=='[')
                {
                    s.push(c);
                }
               else if(s.size()!=0 && ((c=='}' && s.peek()=='{') || (c==')' && s.peek()=='(') || (c==']' && s.peek()=='[')))
               {
                s.pop();
               }
               else{
                s.push(c);
               }
    
            }
            if(s.size()==0) System.out.println("true");
            else System.out.println("false");
        }
    
    }
    

    }

  • + 0 comments

    public static void main(String []argh) { Scanner sc = new Scanner(System.in);

        while (sc.hasNext()) {
            String input=sc.next();
            String[] values = input.split("");
            Stack<String> stack = new Stack<>();
            Map<String, String> chars = new HashMap<>();
            chars.put("(", ")");
            chars.put("{", "}");
            chars.put("[", "]");
            boolean malformed = false;
    
            for (String value: values){
                if (chars.keySet().contains(value)){
                    stack.add(value);
                } else {
                    try{
                        String openingChar = stack.pop();
                        if (!chars.get(openingChar).equals(value)){
                            malformed = true;
                        }    
                    } catch (EmptyStackException e){
                        malformed = true;
                    }
    
                }
            }
            System.out.println(stack.isEmpty() && !malformed ? "true" : "false");
    
            //Complete the code
        }
    
    }
    
  • + 0 comments

    Please this my implementation if anyone note vulnerability please told me

    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                String input = sc.next();
                System.out.println(validate(input));
            }
        }
    
        public static boolean validate(String input) {
            Stack<String> stack = new Stack<String>();
            List<String> characterList = new ArrayList<>(Arrays.asList(input.split("")));
            StringBuilder compareStr = new StringBuilder();
            characterList.forEach(c -> {
                switch (c) {
                    case "(":
                        stack.push(c);
                        compareStr.append(c);
                        break;
                    case "{":
                        stack.push(c);
                        compareStr.append(c);
                        break;
                    case "[":
                        stack.push(c);
                        compareStr.append(c);
                        break;
                    case ")":
                        if (!stack.empty() && stack.peek().equals("(")) {
                            compareStr.append(c);
                            stack.pop();
                        }
                        break;
                    case "}":
                        if (!stack.empty() && stack.peek().equals("{")) {
                            compareStr.append(c);
                            stack.pop();
                        }
                        break;
                    case "]":
                        if (!stack.empty() && stack.peek().equals("[")) {
                            compareStr.append(c);
                            stack.pop();
                        }
                        break;
                }
            });
            if (!compareStr.toString().equals(input)) {return false;}
            if (stack.empty()) {
                return true;
            }
            return false;
        }
    }
    
  • + 0 comments
    import java.util.*;
    class Solution {
        
        public static void main(String[] argh) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                String input = sc.next();
                // Create stack to track opening brackets
                Stack<Character> stack = new Stack<>();
                // Loop through the characters of the string
                boolean isValid = true; // Track if the string is valid
                for (int i = 0; i < input.length(); i++) {
                    Character ch = input.charAt(i);
                    
                    // If it's an opening bracket, push to the stack
                    if (ch == '{' || ch == '(' || ch == '[') {
                        stack.push(ch);
                    } 
                    // If it's a closing bracket
                    else if (ch == '}' || ch == ')' || ch == ']') {
                        // If stack is empty, it's an unmatched closing bracket
                        if (stack.isEmpty()) {
                            isValid = false;
                            break; // Exit the loop early
                        }
                        // Pop from stack and check if the brackets match
                        char top = stack.pop();
                        if ((ch == '}' && top != '{') || 
                            (ch == ')' && top != '(') || 
                            (ch == ']' && top != '[')) {
                            isValid = false;
                            break; // Exit the loop early
                        }
                    }
                }
                
                // After processing all characters, the stack must be empty
                if (isValid && stack.isEmpty()) {
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }
            }
        }
    }