题解 | 好串

好串

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

s = input().strip()

# 检查长度是否为偶数
if len(s) % 2 != 0:
    print("Bad")
    exit()

# 检查是否只包含a和b
for c in s:
    if c not in {'a', 'b'}:
        print("Bad")
        exit()

count_a = 0
count_b = 0

for c in s:
    if c == 'a':
        count_a += 1
    else:
        count_b += 1
    # 任何前缀中a的数量不能少于b的数量
    if count_b > count_a:
        print("Bad")
        exit()

# 检查a和b的总数是否相等
if count_a != count_b:
    print("Bad")
else:
    print("Good")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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