In Part A we’re implementing a method that goes through an array of tokens and adds the delimiters it finds into an ArrayList.

For example, in the string ( x + y ) = ( y + x ) , and assuming that openDel.equals("(") and closeDel.equals(")") , it should return the list ["(", ")", "(", ")"].

public ArrayList<String> getDelimitersList(String[] tokens) {
	ArrayList<String> out = new ArrayList<>();
	for (String t: tokens) {
		if (t.equals(openDel) || t.equals(closeDel))
			out.add(t); 
	}
	return out; 
}

What I did was to iterate through tokens and every time it finds a matching open or closing delimiter it adds it onto the end of out and then returns out at the end.

A statement is considered balanced if

At no point going through the ArrayList have we found more closing delimiters than opening delimiters There are the same number of opening and closing delimiters in the list

public boolean isBalanced(ArrayList<String> delimiters) {
    int open = 0;
    int close = 0;
    for (String d: delimiters) {
        if (d.equals(openDel)) {
            open++;
        } else if (d.equals(closeDel)) {
            close++;
        }

        if (close > open) return false; 
    }
    return open == close; 
}

What I did was to iterate through and count the number of opening and closing delimiters as it goes. If, at any point in the iteration it finds that the code has counted more closing delimiters than opening delimiters it returns false because it has failed the first condition.

At the end it returns true if the two counts match or false if not.