(defun ebi-lang-p (s)
(loop with stack = ()
for c across s
do (cond ((char= c #\A)
(push c stack))
((char= c #\B)
(if stack
(pop stack)
;; 対応する A のない B に出会した
(return nil)))
(t
;; A と B 以外を含む場合は偽とする
(return nil)))
finally (return (null stack))))
(loop for s in '("" "A" "B" "AB" "ABB" "AAB" "AABB" "ABAABBAAABBB" "ABC")
for v = (ebi-lang-p s)
do (format t "~A → ~:[🐟~;🦐~]~%" s v))
KGRlZnVuIGViaS1sYW5nLXAgKHMpCiAgKGxvb3Agd2l0aCBzdGFjayA9ICgpCiAgICAgICAgZm9yIGMgYWNyb3NzIHMKICAgICAgICBkbyAoY29uZCAoKGNoYXI9IGMgI1xBKQogICAgICAgICAgICAgICAgICAocHVzaCBjIHN0YWNrKSkKICAgICAgICAgICAgICAgICAoKGNoYXI9IGMgI1xCKQogICAgICAgICAgICAgICAgICAoaWYgc3RhY2sKICAgICAgICAgICAgICAgICAgICAgIChwb3Agc3RhY2spCiAgICAgICAgICAgICAgICAgICAgICA7OyDlr77lv5zjgZnjgosgQSDjga7jgarjgYQgQiDjgavlh7rkvJrjgZfjgZ8KICAgICAgICAgICAgICAgICAgICAgIChyZXR1cm4gbmlsKSkpCiAgICAgICAgICAgICAgICAgKHQKICAgICAgICAgICAgICAgICAgOzsgQSDjgaggQiDku6XlpJbjgpLlkKvjgoDloLTlkIjjga/lgb3jgajjgZnjgosKICAgICAgICAgICAgICAgICAgKHJldHVybiBuaWwpKSkKICAgICAgICBmaW5hbGx5IChyZXR1cm4gKG51bGwgc3RhY2spKSkpCgoobG9vcCBmb3IgcyBpbiAnKCIiICJBIiAiQiIgIkFCIiAiQUJCIiAiQUFCIiAiQUFCQiIgIkFCQUFCQkFBQUJCQiIgIkFCQyIpCiAgICAgIGZvciB2ID0gKGViaS1sYW5nLXAgcykKICAgICAgZG8gKGZvcm1hdCB0ICJ+QSDihpIgfjpb8J+Qn3478J+mkH5dfiUiIHMgdikpCg==