-3

I'm a total beginner, thus I have this stupid question. Let's say I have a brackets.txt file which has a string like this "(()))))(()()()()()()()()()()()" and I want to write a script that checks whether all of the brackets were closed correctly and there are not too many of them (prints "everything's correct" or something like that). For instance, in "))))((((" each bracket has the matching one but they aren't closed correctly. I know how to find and count them but the checking part is really bugging me, I would be more than grateful for your help!

Acallar
  • 453
  • 1
  • 5
  • 5
  • 1
    Post the code you have tried so far. – iqstatic Oct 07 '14 at 11:19
  • This is a quite common problem. Do some research on "check balanced parantheses". – fedorqui Oct 07 '14 at 11:19
  • I haven't found anything yet for bracket check, although I'm digging awk and grep commands at the moment. – Acallar Oct 07 '14 at 11:19
  • @Accalar - as you're dealing with recursion, i wouldn't use any regex on this, better write a simple loop to check for "leftover brackets", see Sayuris answer – Jeredepp Oct 07 '14 at 11:22
  • Also see [this recent question](http://stackoverflow.com/questions/25852642/why-does-this-solution-fail-nested-and-matching-brackets) – PM 2Ring Oct 07 '14 at 11:49

3 Answers3

5

Just keep removing () until you can't anymore. If anything is left, the input was invalid.

while [[ $string = *'()'* ]] ; do
    string=${string//()/}
done

if [[ $string ]] ; then
    echo Invalid
else
    echo Valid
fi
choroba
  • 231,213
  • 25
  • 204
  • 289
  • This one is really great, however, if the string has something between the brackets (any kind of character) then this doesn't work ): – Acallar Oct 07 '14 at 14:09
  • 1
    @Acallar: Do something like `string=$(tr -cd '()' <<< "$string")` to remove the other characters. – choroba Oct 07 '14 at 14:22
1

Well, this should be the logic.

  1. If the string begins with a ")" - it is not correct
  2. If it begins with a "(" proceed to (3)
  3. For each "(" increment a counter say, 'openBraces'; for each ')' encountered decrement the counter
  4. If at the end of running through (3) above the count of 'openBraces' is 0; all your braces are closed; else something is wrong Note: A positive count of 'openBraces' would indicate an excess of '(' and a negative value of 'openBraces' would indicate an excess of ')'.

Hope it helps

5122014009
  • 3,766
  • 6
  • 24
  • 34
  • 1
    You also need to check that `openBraces` never goes below zero (which would indicate a closing brace without a matching opening brace preceding it. – chepner Oct 07 '14 at 13:13
-1

I think u can try the below code:

    String input = "()))((";
     int count1=0;
     int count2=0;
     boolean temp = false;
     char[] a = new char[input.length()];
     for(int x=0;x<input.length();x++){
         a[x] = input.charAt(x);
         if(a[x]=='('){
             count1++;
         }
         else if(a[x]==')'){
             count2++;
         }
     }
     if(a[0]==')'){
         temp = true;
     }
     else if(a[0]=='(' && count1==count2){
         int t1=1;
         int t2=0;
         for(int x=1;x < input.length();x++){
             if(a[x]=='('){
                 t1++;
             }
             if(a[x]==')'){
                 t2++;
                 if(t2>t1){
                   temp = true;
                   break;
                 }
             }
         }

    }
     else{
        temp = true;
     }
     if(!temp){
         System.out.println("correct");
     }
     else{
         System.out.println("incorrect");
     }
Aksman88
  • 1
  • 1