遇到a入栈,遇到b出栈,判断最后栈是否为空就行。
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Stack<Character> s = new Stack<Character>();
char []charArray = str.toCharArray();
boolean b = true;
for(int i = 0 ;i<charArray.length;i++){
if(charArray[i]=='a'){
s.add('a');
}else if(s.isEmpty()){
b = false;
break;
}else{
s.pop();
}
}
if(b){
if(s.isEmpty()) System.out.println("Good");
else System.out.println("Bad");
}else{
System.out.println("Bad");
}
}
}