This page is still under construction.

Parts of this page are still being built. What you see may change.

Bananas

Interview

Time limit1sMemory limit128 MB

Summary
Decide for each word whether it fits the recursive grammar of a monkey language, where words wrap other words in N and in B...S.
Level

Medium5 of 10

Topics
String, Recursion, Implementation, Backtracking
Solved
No attempts yet

Problem

The term "code monkey" is sometimes used for a programmer who does not know much about programming. This is unfair to monkeys: contrary to popular belief, monkeys are quite smart — they have simply been misunderstood. Perhaps this is because monkeys do not speak English, only monkey language. Your job is to help humans and monkeys understand each other by building a monkey-language dictionary: for each word you are given, decide whether it is a valid monkey-language word.

Spelling in monkey language is very simple. A string is a monkey-language word if and only if it can be built with the following rules:

  1. A monkey-language word is an A-word, optionally followed by the letter N and another monkey-language word.
  2. An A-word is either the single letter A, or the letter B followed by a monkey-language word followed by the letter S.

For example:

  • A is a monkey-language word because it is an A-word.
  • ANA is a monkey-language word: the A-word A, then N, then the monkey-language word A.
  • ANANA is a monkey-language word: the A-word A, then N, then the monkey-language word ANA.
  • BANANAS is a monkey-language word: it is an A-word, namely B followed by the monkey-language word ANANA followed by S.

Input

Each line contains one word made of uppercase letters. Reading stops at the word X; X itself is not a monkey-language word and produces no output.

Output

For each word before X, print YES if it is a valid monkey-language word, or NO if it is not, one answer per line.

Examples1

  1. Example 1

    Input
    A
    ANA
    ANANA
    BANANAS
    BANANA
    X
    
    Expected output
    YES
    YES
    YES
    YES
    NO