Skip to main content
1 of 6
Beefster
  • 10k
  • 35
  • 102

Is It Mountainous?

#Challenge

For this challenge, a mountainous string is one that conforms to the grammar rule M: x(Mx)* where at each production, the all x's are the same character. When indented, a mountainous string might look something like this:

A B C D C E F E C B A 

As you can see, it looks a bit like a mountain from the side.

#Formal Definition

  • Any single character a is mountainous.
  • If S is a mountainous string and a is a character, then aSa is mountainous, where juxtaposition represents string concatenation.
  • If aSa and aTa are mountainous strings, then aSaTa is a mountainous string. Note that this rule implies that this pattern holds for any number of repetitions. (i.e. aSaTaUa, aSaTaUaVa, aSaTaUaVaWa... are all mountainous.)

#Examples

All odd-length palindromes are mountainous, for instance:

t a c o c a t 

qwertytrasdfdgdsarewqjklkjq is a less trivial example:

q w e r t y t r a s d f d g d s a r e w q j k l k j q 

#Example Outputs

a ==> true mom ==> true tacocat ==> true qwertytrasdfdgdsarewqjklkjq ==> true abcbcbcbcba ==> true <empty string> ==> false aa ==> false pie ==> false toohottohoot ==> false asdfdghgfdsa ==> false wasitacaroraratisaw ==> false myhovercraftisfullofeels ==> false 

#Rules

  • This is a decision problem, so any representation of true or false is valid output as long as it is correct, consistent, and the program terminates in a finite amount of time. Be sure to state your output convention with your solution.
  • This is code golf, so the shortest program wins.
  • Standard loopholes are banned.
Beefster
  • 10k
  • 35
  • 102