Develop a program to check if a given word is part of a language defined by the following rules:
- The language has a set of rules.
- The empty string is considered a valid word in the language.
- Two words from the language can be concatenated to create a new word in the language.
- If X is a word in the language, then "A" + X + "A" is also a word in the language.
- If X is a word in the language, then "A" + X + "C" is also a word in the language.
- If X is a word in the language, then "B" + X + "A" is also a word in the language.
- If X is a word in the language, then "B" + X + "C" is also a word in the language.
You can assume all inputs only consist of the characters A, B and C.
Examples:
The word "CB" doesn't belong to the language. There is no way to arrive at this word from the above rules.
The word "BAAC" belongs to the language.
Reasoning: The empty string is a valid word. Applying the "A" + X + "A" rule, we get "AA". Now we apply the "B" + X + "C" rule to get "BAAC".
The word "BCBC" belongs to the language.
Reasoning: The empty string is a valid word. Applying the "B" + X + "C" rule, we get "BC". Now we apply the concatenation rule to get "BCBC". The concatenation rule can be used to concatenate a word with itself.
Test cases
CBAA ---> false
CBBB ---> false
BCCA ---> false
CCAC ---> false
ABAC ---> true
ACAB ---> false
AAAC ---> true
BBAC ---> true
CABC ---> false
CCAB ---> false
This is a code-golf problem. While the language looks random, I hope you can find a pattern in the language to write the shortest code possible. Hint: It is possible to check in linear time.