What is the time complexity of the following code? public boolean…
PYQ AMCAT 2024
What is the time complexity of the following code?
public boolean isBalanced(String exp)
{
int len = exp.length();
Stack <Integer> stk = new Stack<Integer>();
for(int i = 0; i < len; i++)
{
char ch = exp.charAt(i);
if (ch == '(')
stk.push(i);
else if (ch == ')')
{
if(stk.peek() == null)
{
return false;
}
stk.pop();
}
}
return true;
}
- A.
O(logn)
- B.
O(n)
- C.
O(1)
- D.
O(nlogn)
Attempted by 298 students.
Sign up free to check your answer
Sign up freeLoading lesson…

