A small variation of [a challenge on CGCC.SE](https://codegolf.stackexchange.com/q/85994/78410). ## Task Given a string $s$ made of only lowercase a's and b's, determine if it satisfies $$ s \in \{ a^n b^n a^n | n \in \mathbb{Z}, n \ge 0 \} $$ In plain English, determine if $s$ equals some (possibly zero) copies of `a`, followed by the same number of `b`, followed by the same number of `a`. ## Input and output A full program or a function is acceptable. You can take input as a string, a list of characters, or a list of codepoints (which will then be a list of 97 and 98). You can choose to output one of the following: * Truthy / falsy values following your language's convention (swapping is also allowed) * Two distinct values representing true / false respectively ## Test cases | Input | Output | | :-- | :-: | | ` ` (empty string) | true | | `aba` | true | | `aabbaa` | true | | `aaabbbaaa` | true | | `a` | false | | `b` | false | | `ab` | false | | `ba` | false | | `aab` | false | | `aabbaab` | false | | `aabbaaa` | false | | `aaabbaa` | false | | `baabbaa` | false | | `aabbbaa` | false |

or

# [APL (Dyalog Unicode)], 17 [bytes](https://codegolf.meta.stackexchange.com/a/9429/43319 "When can APL characters be counted as 1 byte each?")^SBCS^ Anonymous tacit prefix function. Requires 0-based indexing (`⎕IO←0`) ```apl ⊂∊'aba'/¨∘⊂⍨∘⍳1⌈≢ ``` [Try it online!][TIO-k60fj353] | Reading order| Notes | ---- | ----- | `⊂∊` |Is the whole argument a member of |   `'aba'/¨∘⊂`|the whole of "aba", with its letters replicated by each |            `∘` | of |             `⍳` | 0, 1, … |              `1⌈` | the maximum of 1 and |               `≢` | the tally of characters? | Execution order| Notes | ---- | ----- |               `≢` | tally the argument |              `1⌈` | the maximum of 1 and that |             `⍳` | 0, 1, … that |            `∘` | then |   `'aba'/¨∘⊂`|For each of those, replicate the characters of "aba" | `⊂∊` |is the whole argument a member of that list? [APL (Dyalog Unicode)]: https://www.dyalog.com/ [TIO-k60fj353]: https://tio.run/##SyzI0U2pTMzJT///qG@qp/@jtgkG/x91NT3q6FJPTEpU1z@04lHHDJBAL5jRu9nwUU/Ho85F/9OASh/19kF0dTUfWm/8qG0ikBcc5AwkQzw8g/@nqatzpYGNAVGJSUmJUBaQCWRDOCAiCaIOzIQpRuhBYiL0Q5hJyKaCDVUHAA "APL (Dyalog Unicode) – Try It Online"

# [Golfscript](http://www.golfscript.com/), 26 bytes ``` ..3/,/{.|}/~++"aba"=\,3%!* ``` Not particularly optimal, but the first solution I thought of. Probably could pare down the string-comparison bit into some kind of bitwise comparison. ~++"aba"= is 9 bytes just to check "is the order right", when it could probably be done in 3 somehow. After the loop, we would have (if correct), ["a", "b", "a"], so it should be doable. I'll check some bitwise comparisons later, but this is a good starting benchmark. Parse; ``` .. #Duplicate the string twice. Stack is now three copies. 3/ #Divide the string into parts of size 3. , #Count how many parts there are, and get rid of that copy / #Divide one of the two original strings into three parts #using the prev number {.|}/ #For each of these parts, get rid of duplicates ~++ #Break them out of the array then concatenate them. "aba"= #Make sure the array followed the pattern of #1/3 a's, 1/3 b's, then 1/3 a's again. #If so, we replace it with 1. Otherwise 0. \ #Bring that last original-string over. ,3% #Make sure it's a clean multiple of three. ! #If so, 1. If not, 0. * #Multiply! 1*1=1, and 0*1 or 0*0=0, so if #both conditions pass, we pass 1. ```

## [Haskell], 26 bytes ```haskell f s=[c|c<-"aba",'b'<-s]==s ``` [Try it online!][TIO-k61uvjuc] [Haskell]: https://www.haskell.org/ [TIO-k61uvjuc]: https://tio.run/##TYxBCoAwDAS/EoLgxf7AvkQ8JMWqWKUYj77dWK2il2QWZncgmboQVD2IbdzuaoPEhFXJZW2ktVZ0pnEBC3Edlw0KmCmChwaxgqymR8z0UMLEOVyHs3fjK3@dH379jPxfvUex1cP5QL2ocTGe "Haskell – Try It Online" Checks that the input string is equivalent to the following construction: for each character in `"aba"`, repeat that character once for each `'b'` in the input (checked with the pattern match `'b'<-s`). An equally-long alternative uses `<*`. ```haskell f s=("aba"<*[1|'b'<-s])==s ``` [Try it online!][TIO-k61v36zg] [Haskell]: https://www.haskell.org/ [TIO-k61v36zg]: https://tio.run/##TYxLCoRADESvEoLgB124t08iLhKZVrGVZuLSsxu1ewbdJK/gVY0k88c5VQtiMiQmbIq23lNOm0q63BjRhaYVDPjvtG6QwEIeLLSIJQT/fsRMP7rw4hjuw9EL@JefzguffkR@r4ZR7PToraNBtOq9PwE "Haskell – Try It Online"