Advertisement
Guest User

Untitled

a guest
Dec 12th, 2013
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. /**
  2.      * checkBalance - an aid method:
  3.      * will check if a given string is balanced, recursively, with the help of another strings
  4.      * @param str1 - the given string to check.
  5.      * @param openBrackets - will store the open brackets found in str1
  6.      * @return true - if the given string is balanced, false - if the given string isn't balanced.
  7.      */
  8.     public static boolean checkBalance(String str1, String openBrackets){
  9.  
  10.         int a = bracketIndex(str1);
  11.  
  12.         if ( a == -1 ){
  13.             return openBrackets.isEmpty();
  14.         }//if
  15.  
  16.         if( isItOpen(str1.charAt(a)) == true )
  17.         {
  18.             return checkBalance(str1.substring(a+1), openBrackets + str1.charAt(a));
  19.         }//if
  20.         else{
  21.             if ( openBrackets.length() == 0){
  22.                 return false;
  23.             }//if
  24.            
  25.             if( checkBracket( openBrackets.charAt(openBrackets.length()-1), str1.charAt(a) ) == true ) {
  26.                 return checkBalance(str1.substring(a+1), openBrackets.substring(0, openBrackets.length()-1));
  27.             }//if
  28.  
  29.         }//else
  30.  
  31.         return false;
  32.     }//end of checkBalance
  33.    
  34.     /**
  35.      * isBalanced - 3rd Method:
  36.      * will check if a given string is balanced, using an aid method that gets two strings (according to the hint)
  37.      * @param str - the string to be checked.
  38.      * @return true - if the given string is balanced.
  39.      */
  40.     public static boolean isBalanced(String str) {
  41.         return checkBalance(str, "");
  42.     }//end of isBalanced
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement