Parenthesis/Brackets Matching using Stack algorithm

前端 未结 30 2769
你的背包
你的背包 2020-11-27 11:39

For example if the parenthesis/brackets is matching in the following:

({})
(()){}()
()

and so on but if the parenthesis/brackets is not mat

30条回答
  •  我在风中等你
    2020-11-27 12:04

    //basic code non strack algorithm just started learning java ignore space and time.
    /// {[()]}[][]{}
    // {[( -a -> }]) -b -> replace a(]}) -> reverse a( }]))-> 
    //Split string to substring {[()]}, next [], next [], next{}
    
    public class testbrackets {
        static String stringfirst;
        static String stringsecond;
        static int open = 0;
        public static void main(String[] args) {
            splitstring("(()){}()");
        }
    static void splitstring(String str){
    
        int len = str.length();
        for(int i=0;i<=len-1;i++){
            stringfirst="";
            stringsecond="";
            System.out.println("loop starttttttt");
            char a = str.charAt(i);
        if(a=='{'||a=='['||a=='(')
        {
            open = open+1;
            continue;
        }
        if(a=='}'||a==']'||a==')'){
            if(open==0){
                System.out.println(open+"started with closing brace");
                return;
            }
            String stringfirst=str.substring(i-open, i);
            System.out.println("stringfirst"+stringfirst);
            String stringsecond=str.substring(i, i+open);
            System.out.println("stringsecond"+stringsecond);
            replace(stringfirst, stringsecond);
    
            }
        i=(i+open)-1;
        open=0;
        System.out.println(i);
        }
        }
        static void replace(String stringfirst, String stringsecond){
            stringfirst = stringfirst.replace('{', '}');
            stringfirst = stringfirst.replace('(', ')');
            stringfirst = stringfirst.replace('[', ']');
            StringBuilder stringfirst1 = new StringBuilder(stringfirst);
            stringfirst = stringfirst1.reverse().toString();
        System.out.println("stringfirst"+stringfirst);
        System.out.println("stringsecond"+stringsecond);
    if(stringfirst.equals(stringsecond)){
        System.out.println("pass");
    }
        else{
            System.out.println("fail");
            System.exit(0);
            }
        }
    }
    

提交回复
热议问题