(b) Write the method isBalanced, which returns true when the delimiters are balanced and returns false otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise they are not balanced.
When traversing the ArrayList from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.
Consider a Delimiters object for which openDel` is "<sup>" and closeDel is "</sup>". The examples below show different ArrayList objects that could be returned by calls to getDelimitersList and the value that would be returned by a call to isBalanced.
Write the method isBalanced, which returns true when the delimiters are balanced and returns false otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise they are not balanced.
When traversing the ArrayList<String> delimiters from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.
This section contains a plain English explanation of one way to solve this problem as well as problems that test your understanding of how to write the code to do those things.
The method isBalanced will loop through delimiters and keep track of the number of open and close delimiters we have found so far. To do that we can create two integer variables: totalOpen and totalClose and set them to 0 initially. Each time through the loop we will check if the current string which we will call currString is equal to openDel and if so increment totalOpen, otherwise if it is equal to closeDel increment totalClose. Next if totalClose > totalOpen the method should return false. A After the loop return totalOpen == totalClose. This will return true if they are equal and false otherwise.
Write the method isBalanced, which returns true when the delimiters are balanced and returns false otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise they are not balanced.
When traversing the ArrayList from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.