My issue
Can some one review my code and tell why i am getting an empty line in the first line of output and the rest of testcases are correct.
My code
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String exp = sc.nextLine();
Stack<Character> stack = new Stack<>();
String ans = "";
for(int i = 0; i < exp.length(); i++){
char c = exp.charAt(i);
if(c >= 'a' && c <= 'z'){
ans += c;
}
else if(c == '('){
stack.push(c);
}
else if(c == ')'){
while(stack.peek() != '('){
ans += stack.peek();
stack.pop();
}stack.pop();
}
else {
stack.push(c);
}
}
System.out.println(ans);
}
}
}
Learning course: Stacks and Queues
Problem Link: Practice problem - Transform the Expression Practice Problem in Stacks and Queues - CodeChef