Task:

 Generate a string with   N   opening brackets   [   and with   N   closing brackets   ],   in some arbitrary order. 
 Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.


Examples

   (empty)      OK
   []           OK   
   [][]         OK   
   [[][]]       OK 
   ][         NOT OK
   ][][       NOT OK
   []][[]     NOT OK



'Altered to prevent lines starting with ']' or ending with '[' being generated as they can't work
 
siNumberOfBrackets As Short = 20              'Maximum amount of brackets in a line
siNumberOfLines As Short = 20                 'Amount of lines to test
 
'----
 
Public Sub Main()
Dim sBrks As String[] = GenerateBrackets()    'Get random array to check
Dim sTemp, sHold, sWork As String             'Working variables
Dim siCount As Short                          'Counter
 
For Each sTemp In sBrks                       'For each line in the sBrk array (e.g. '[][][][[[[]][]]]')
  sWork = sTemp                               'Make sWork = sTemp
  Repeat                                      'Repeat
    sHold = sWork                             'Make sHold = sWork
    sWork = Replace(sWork, "[]", "")          'Remove all brackets that match '[]'
  Until sHold = sWork                         'If sHold = sWork then there are no more '[]' matches
 
  If sWork = "" Then                          'So if all the brackets 'Nested' correctly sWork will be empty
      Print "    OK ";                        'Print 'OK'
  Else                                        'Else they did not all match
    Print "NOT OK ";                          'So print 'NOT OK'
  Endif
 
  For siCount = 1 To Len(sTemp)               'Loop through the line of brackets
    Print Mid(sTemp, siCount, 1) & " ";       'Print each bracket + a space to make it easier to read
  Next
  Print                                       'Print a new line
Next
 
End
 
'----
 
Public Sub GenerateBrackets() As String[]     'Generates an array of random quantities of '[' and ']'
Dim siQty As New Short[]                      'To store the random number (of brackets) to put in a line
Dim sBrk As New String[]                      'To store the lines of brackets
Dim siNum, siEnd, siLoop As Short             'Various counters 
Dim sTemp As String                           'Temp string
 
Repeat                                        'Repeat
  siNum = Rand(0, siNumberOfBrackets)         'Pick a number between 0 and the total number of brackets requested
  If Even(siNum) Then siQty.Add(siNum)        'If the number is even then add the number to siQty
Until siQty.Count = siNumberOfLines           'Keep going until we have the number of lines requested
 
For Each siNum In siQty                       'For each number in siQty..(e.g. 6)
  Do
    siEnd = Rand(0, 1)                        'Generate a 0 or a 1
    If siEnd = 0 Then sTemp &= "["            'If '0' then add a '[' bracket
    If siEnd = 1 Then sTemp &= "]"            'If '1' then add a ']' bracket
 
    If siNum = 0 Then                         'If siNum = 0 then..
      sBrk.Add("")                            'Add '0' to the array
        sTemp = ""                            'Clear sTemp
        Break                                 'Exit the Do Loop
    Endif
 
    If Len(sTemp) = siNum Then                'If the length of sTemp = the required amount then..
      If sTemp Not Begins "]" And sTemp Not Ends "[" Then  'Check to see that sTemp does not start with "]" and does not end with a "["
        sBrk.Add(sTemp)                       'Add it to the array
        sTemp = ""                            'Clear sTemp
        Break                                 'Exit the Do Loop
      Else                                    'Else
        sTemp = ""                            'Clear sTemp
      End If                                  'Try again!
    Endif
  Loop
Next
 
Return sBrk                                   'Return the sBrk array
 
End