• + 0 comments

    JAVA 8

    public static String isBalanced(String s) { String response = "YES"; String[] values = s.split(""); String listExpected = ""; String passValues = "({[";

    for(int i = 0; i < values.length; i ++){
        String value = values[i];
    
        if(!passValues.contains(value)){
            String[] list = listExpected.split("");
            if(list[0].equals(value)){
                listExpected = listExpected.replaceFirst("\\"+value, "");
            } else {
                response = "NO";
                break;
            }
        }
    
        if(value.equals("(")){
            listExpected = ")" + listExpected;
        }
    
        if(value.equals("{")){
            listExpected = "}" + listExpected;
        }
    
        if(value.equals("[")){
            listExpected = "]" + listExpected;
        }
    }
    
    return response;
    }