Delimiters free response

Delimiters free response problem from the 2019 AP Computer Science A Exam.

Delimiters is #3 from the from the 2019 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/pdf/ap19-frq-computer-science-a.pdf?course=ap-computer-science-a

Part (a) – getDelimitersList method

public ArrayList<String> getDelimitersList(String[] tokens)
{
  ArrayList<String> delimiters = new ArrayList<String>();

  for(String t : tokens)
    if(t.equals(openDel) || t.equals(closeDel))
      delimiters.add(t);
        
  return delimiters;
}

Part (b) – isBalanced method

public boolean isBalanced(ArrayList<String> delimiters)
{
  int numOpen = 0, numClosed = 0;

  for(String d : delimiters)
  {
    if(d.equals(openDel))
      numOpen++;

    if(d.equals(closeDel))
      numClosed++;
            
    if(numClosed > numOpen)
      return false;
  }
        
  return numOpen == numClosed;
}

2019 AP CS Exam Free Response Answers