Sort by

recency

|

636 Discussions

|

  • + 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");
                }
            }
        }
    }
    
  • + 0 comments

    import java.util.*; class Solution{ public static boolean check(String s) { Stack stack = new Stack<>(); for (char ch : s.toCharArray()) {
    if (ch == '(' || ch == '{' || ch == '[') { stack.push(ch); } else if (ch == ')' || ch == '}' || ch == ']') { if (stack.isEmpty()) return false; char top = stack.pop(); if ((ch == ')' && top != '(') || (ch == '}' && top != '{') || (ch == ']' && top != '[')) { return false; } } } return stack.isEmpty(); }

    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String input=sc.next();
            //Complete the code
            if(check(input)) System.out.println("true");
            else System.out.println("false");
        }
    
    }
    

    }

  • + 0 comments
    import java.util.*;
    class Solution{
    	public static boolean isValid(String str)
        {
            String open = "{[(";
            String close = "}])";
            Stack<Character> stack = new Stack<>();
    
            for (char c : str.toCharArray()) {
                if (open.indexOf(c) == -1 && close.indexOf(c) == -1)
                    {return false;}
                else if (open.indexOf(c) != -1) {stack.push(c);}
                else
                    {char opposite = open.charAt(close.indexOf(c));
                    if (stack.isEmpty() || stack.pop() != opposite) return false;
                    }
                }
            return stack.isEmpty();   
        }
    	public static void main(String []argh)
    	{
    		Scanner sc = new Scanner(System.in);
    		while (sc.hasNext()) {
    			String input=sc.next();
                System.out.println(isValid(input));
    		}
    		
    	}
    }
    
  • + 0 comments

    Java 15+ (using my own Stack class, I forgot that already exists this class :P)

    import java.util.*;
    
    class Stack {
            
        private List<Character> elements;
    
        public Stack() {
            this.elements = new ArrayList<Character>();
        }
            
        public Stack(List<Character> elements) {
            this.elements = new ArrayList<>(elements);
        }
        
        public int size() {
            return this.elements.size();
        }
            
        public void push(Character element) {
            this.elements.add(element);
        }
            
        public Character pop() {
            if (isEmpty())
                return null;
     
            var element = elements.get(size() - 1);
            elements.remove(size() - 1);
            return element;
        }
        
        public boolean isEmpty() {
            return size() == 0;
        }
    
    }
    
    public class Solution {
    
        public static void main(String[] args) {
            
            var matches = Map.of('{', '}', '[', ']', '(', ')');
            var openElems = matches.keySet();
            
            try (var scanner = new Scanner(System.in)) {
                
                while(scanner.hasNext()) {
                    
                    var line = scanner.nextLine();
                    
                    var closeStack = new Stack();
                    
                    if (line.length() % 2 != 0) {
                        System.out.println(false);
                        continue;
                    }
                    
                    var result = true;
                    var index = line.length() - 1;
                    while (index >= 0) {
                        var c = line.charAt(index);
                        if (openElems.contains(c)) {
                            var closeElem = closeStack.pop();
                            if (matches.get(c) != closeElem) {
                                result = false;
                                break;
                            } 
                        }
                        else
                            closeStack.push(c);
                            
                        index--;
                    }                 
    
                    if (closeStack.size() > 0)
                        result = false;
    
                    System.out.println(result);
                }
            }
        }
    }