can you please correct my code(which is in the end of the question) and make sure if it follows all the question below strategies ??: . I dont’t have a testing code we have to build one.
A stack is a data structure where data is accessed using the LIFO (last in first out) principle. In this problem, you will use a stack to check whether a string has balanced parentheses (, ) and brackets {, }, [, ], or not.
A string that has balanced parentheses and brackets will be said to be balanced. Any character that is not one of (, ), [, ], {, or } is not important when deciding if a string is balanced and can be ignored.
We will define balanced as follows. A string str is balanced
if str does not contain a parenthesis or bracket symbol, or
str consists of a balanced string surrounded by opening and closing parentheses or matching brackets. That is, str is (b), {b} or [b], where b is any balanced string, or
if str is the concatenation of any two balanced strings. That is, str is bc, where b and c are any balanced strings.
You will complete the provided Balanced class that has two static methods isBalanced(String) and numberOfBalancedStrings(String[]).
Your isBalanced method must use the java.util.Stack class (in a way that solves the problem) to receive any grades for this problem.
http://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Examples
The following strings have balanced parentheses
(), ()()
cat, c(at), (hello)(kitty)
if( ((x-y) < 4) || (x > 12))
()(((s)))()()()()(x()((y))(x))()(ccccc(w))ssss()
The following strings do not have balances parentheses
), )(a), )a(
The following strings have balanced parentheses and brackets
a, [], {}, [()], []{}({[{}]}),
for(int i=0; i<12; i+=1){x[i]+=f(1);}
The following strings do not have balanced parentheses and brackets
(], {), [}, [}, (], ({)}h, [(]())
for(int i=0; i<12; i+=1){
Note: You will receive partial marks if you code only works for parentheses (and not brackets).
The code :
import java.util.Stack;
public class Balanced {
/**
* Checks if a string is balanced or not.
*
* @param s
* is a non-null string
* @return true of the input <code>s</code> is balanced and false otherwise.
**/
public static boolean isBalanced(String s) {
Stack<Character> st = new Stack<>();
// for i goes from 0 to string s’s length do char c equal to string s.charAt(i).
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i); //returns the character located at the String’s specified index.
// if charcter c =”)”or charcter c =”]” or charcter c =”}” push character c.
if (c == ‘)’ || c == ‘]’ || c == ‘}’) {
st.push(c);
}
// if charcter c =”(“or charcter c =”[” or charcter c =”{” push character c.
if (c == ‘(‘ || c == ‘[‘ || c == ‘{‘) {
st.push(c);
}
} // ending for forloop
return st.isEmpty();
}
/**
* Counts the number of balanced strings in the input array.
*
* @param in
* is a non-null array of strings
* @return the number of strings in the input <code>in</code> that are
* balanced.
**/
public static int numberOfBalancedStrings(String[] in) {
int count = 0; // set count equal to zero.
// foreach string in the input if the strings are balenced return the number of the balanced strings.
for (String str : in) {
if (isBalanced(str)) {
count++;
}
}
return count;
}
}
Expert Answer
Your code might be wrong and very difficult to understand. I would suggest you use write code. Instead of just counting it is much better to use the stack operations of push and pop. Whenever you encounter (, [ or { you should push it on stack and when ever you encounter ), } or ] pop from stack and match if they are same or not. I am providing you with the checking function only as I don’t know Java but I know C if else or looping constructs are pretty much similar. Please try to implement this without seeing my code. It really helps. Hope this helps. Cheers!
stk is the name of stack, push and pop are stack operations, str.length calculates the length of the string. b is a boolean set to true declared before this. isEmpty is a function that returns true if the stack is empty else false.
for(int i=0; i<str.length() || b;i++)
{
if(str.charAt(i) == ‘(‘ || str.charAt(i) == ‘{‘ || str.charAt(i) == ‘[‘ )
{
stk.push(str.charAt(i));
}
else if(str.charAt(i) == ‘)’ || str.charAt(i) == ‘}’ || str.charAt(i) == ‘]’ )
{
char c= stk.pop();
if(c != str.charAt(i))
{
b=false;
break;
}
}
}
if(b && !stk.isEmpty())
printf(“The String is valid”);
else
printf(“The string is invalid”);