Bananas
InterviewTime limit1sMemory limit128 MB
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:
- A monkey-language word is an A-word, optionally followed by the letter
Nand another monkey-language word. - An A-word is either the single letter
A, or the letterBfollowed by a monkey-language word followed by the letterS.
For example:
Ais a monkey-language word because it is an A-word.ANAis a monkey-language word: the A-wordA, thenN, then the monkey-language wordA.ANANAis a monkey-language word: the A-wordA, thenN, then the monkey-language wordANA.BANANASis a monkey-language word: it is an A-word, namelyBfollowed by the monkey-language wordANANAfollowed byS.
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.