Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * checkBalance - an aid method:
- * will check if a given string is balanced, recursively, with the help of another strings
- * @param str1 - the given string to check.
- * @param openBrackets - will store the open brackets found in str1
- * @return true - if the given string is balanced, false - if the given string isn't balanced.
- */
- public static boolean checkBalance(String str1, String openBrackets){
- int a = bracketIndex(str1);
- if ( a == -1 ){
- return openBrackets.isEmpty();
- }//if
- if( isItOpen(str1.charAt(a)) == true )
- {
- return checkBalance(str1.substring(a+1), openBrackets + str1.charAt(a));
- }//if
- else{
- if ( openBrackets.length() == 0){
- return false;
- }//if
- if( checkBracket( openBrackets.charAt(openBrackets.length()-1), str1.charAt(a) ) == true ) {
- return checkBalance(str1.substring(a+1), openBrackets.substring(0, openBrackets.length()-1));
- }//if
- }//else
- return false;
- }//end of checkBalance
- /**
- * isBalanced - 3rd Method:
- * will check if a given string is balanced, using an aid method that gets two strings (according to the hint)
- * @param str - the string to be checked.
- * @return true - if the given string is balanced.
- */
- public static boolean isBalanced(String str) {
- return checkBalance(str, "");
- }//end of isBalanced
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement