-2

I am checking balanced brackets in string using string. I have made implement stack using class. Firstly when i had run this program using util package it was giving correct answer but when i made custom stack this is giving wrong outputs.What i am making wrong in this..

import java.util.Scanner;

public class Stack {
    int top;
    char []a=new char[10];
    
    public void push(char c)
     {
        if(top <a.length-1)
         {
            top++;
             a[top]=c;
         }
     }
    
    public char pop()
     {
         if(top > 0)
         {
             top--;
             char c=a[top];
             return c;
         }
        return 0;
         
     }
    
    public boolean isEmpty()
     {
        return (top==-1);
     }
    
    public char peek()
     {
        return a[top];
     }
    
    void displayStack()
    {
        for(int i=0;i<=top;i++)
          System.out.print(a[i]+" ");
    }
    public static boolean CheckParentesis(String str)
    {
        if (str.isEmpty())
            return true;

        Stack stack = new Stack();
        for (int i = 0; i < str.length(); i++)
        {
            char Symbol = str.charAt(i);
            if (Symbol == '{' || Symbol == '(' || Symbol == '[')
            {
                stack.push(Symbol);
                continue;
            }
            
            if (Symbol == '}' || Symbol == ')' || Symbol == ']')
            {
                if (stack.isEmpty())
                    return false;

                char last = stack.peek();     //peek checks top element of stack without removing it...
                if (Symbol == '}' && last == '{' || Symbol == ')' && last == '(' || Symbol == ']' && last == '[')
                    stack.pop();
                     
                else 
                    return false;
            }
        
        }
        return stack.isEmpty();
    }
    
    public static void main(String[] args) {
        {  
            Scanner sc = new Scanner(System.in);          
            String[] str = new String [sc.nextInt()];      
            //consuming the <enter> from input above  
            sc.nextLine();   

            for (int i = 0; i < str.length; i++)   
            {  
              str[i] = sc.nextLine();  
            } 
            for(String s: str)   
            {  
                if(CheckParentesis(s)==true)
                       System.out.println("TRUE"); 
                   else
                      System.out.println("FALSE");
            }  
        }  
        
    }
}

Sample Input:

4

(the[is]{valid})

(the[is]{valid))

{the(is[valid])}

(this](is}{valid)

Sample Output:

TRUE

FALSE

TRUE

FALSE

  • 1
    "Why my code isn't working?" isn't sort of question that we want here. Can you be more specific? Did you try to debug it? – talex Nov 23 '21 at 18:29

1 Answers1

0

Mistakes

  1. you didn't use static function or global variables
  2. you have to reset top to -1 after every new string check
  3. while doing pop() you should first capture the element and then decrement the stack index value
import java.util.Scanner;

public class Stack {
    static int top = -1;
    static char[] a = new char[10];

    public static void clear() {
        top = -1;
    }

    public static void push(char c) {
        if (top < a.length - 1) {
            top++;
            a[top] = c;
        }
    }

    public static char pop() {
        if (top >= 0) {

            char c = a[top];
            top--;
            return c;
        }
        return 0;

    }

    public static boolean isEmpty() {
        return (top == -1);
    }

    public static char peek() {
        return a[top];
    }

    static void displayStack() {
        for (int i = 0; i <= top; i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }

    public static boolean CheckParentesis(String str) {
        if (str.isEmpty())
            return true;

        Stack stack = new Stack();
        stack.clear();
        for (int i = 0; i < str.length(); i++) {
            // stack.displayStack();
            char Symbol = str.charAt(i);
            if (Symbol == '{' || Symbol == '(' || Symbol == '[') {
                stack.push(Symbol);
                continue;
            }

            if (Symbol == '}' || Symbol == ')' || Symbol == ']') {
                if (stack.isEmpty())
                    return false;

                char last = stack.peek(); //peek checks top element of stack without removing it...
                if (
                    (Symbol == '}' && last == '{') ||
                    (Symbol == ')' && last == '(') ||
                    (Symbol == ']' && last == '[')
                )
                    stack.pop();

                else
                    return false;
            }

        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        {
            Scanner sc = new Scanner(System.in);
            String[] str = new String[sc.nextInt()];
            //consuming the <enter> from input above  
            sc.nextLine();

            for (int i = 0; i < str.length; i++) {
                str[i] = sc.nextLine();
            }
            for (String s: str) {
                if (CheckParentesis(s) == true)
                    System.out.println("TRUE");
                else
                    System.out.println("FALSE");
            }
        }

    }
}
Ankit Kumar
  • 139
  • 8