题解 | 好串

好串

https://www.nowcoder.com/practice/9b072237ebdd4dd99562f01cbf594fac

import java.util.Scanner;
import java.util.Stack;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Stack<Character>  statck = new Stack<Character> ();
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        for(int i= 0; i< str.length() ;i++){
            if(!statck.isEmpty() && statck.peek() == 'a' && str.charAt(i)=='b'){
                statck.pop();
            }else{
                statck.push(str.charAt(i));
            }
        

        }
        System.out.println(statck.isEmpty()?"Good":"Bad");
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务