Code Golf
string decision-problem add tag
Bubbler
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 |
Top Answer Haskell
xnor
## [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"
Answer #2 APL (Dyalog Unicode)
Adám
# [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
| &emsp;&emsp;`'aba'/¨∘⊂`|the whole of "aba", with its letters replicated by each
| &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`∘`  | of
| &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`⍳`  | 0, 1, …
| &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`1⌈` | the maximum of 1 and
|&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`≢`  | the tally of characters?

| Execution order| Notes
| ---- | -----
|&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`≢`  | tally the argument
| &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`1⌈` | the maximum of 1 and that
| &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`⍳`  | 0, 1, … that
| &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;`∘`  | then
| &emsp;&emsp;`'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"
Answer #3 golfscript
Mathgeek
# [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.
```
Answer #4 CJam
EsolangingFruit
# CJam, 13 bytes

    q_,3/"aba"e*=

[Try it online!](https://tio.run/##S85KzP1fHZ@gpKBrp6Dk8L8wXsdYXykxKVEpVcv2v19tsXqhbl2hn77qfy6gIFdiYlJSIogC0kAGkMWVBJTggkhBpWE0VB2QToLrA2sDAA "CJam – Try It Online")

## Explanation

    q               read the input                     "aabbaa"
      ,             length                             6
       3/           divide by 3                        2
         "aba"e*    repeat elements in string "aba"    "aabbaa"
     _          =   equal to original?                 1
Answer #5 W
petStorm
# [W](https://github.com/A-ee/w) [`d`](https://codegolf.meta.stackexchange.com/questions/14337/command-line-flags-on-front-ends/14339#14339), 11 [bytes](https://github.com/A-ee/w/wiki/Code-Page)
```
←▬┴F║`πδE;L
```
Uncompressed:
```
ck3/*"aba"NJ=
```
# Explanation
```
          N   % Map
     "aba"    % the string aba
ck            % The length of the input
  3/          % Divide by 3
    *         % Repeat input by that
           J  % Join the output
            = % Is it equal to original?
```
Answer #6 Stax
recursive
# [Stax](https://github.com/tomtheisen/stax), 9 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)

	â╢<╜◘@*\@

[Run and debug it](https://staxlang.xyz/#p=83b63cbd08402a5c40&i=%22%22%0A%22aba%22%0A%22aabbaa%22%0A%22aaabbbaaa%22%0A%22a%22%0A%22b%22%0A%22ab%22%0A%22ba%22%0A%22aab%22%0A%22aabbaab%22%0A%22aabbaaa%22%0A%22aaabbaa%22%0A%22baabbaa%22%0A%22aabbbaa%22&a=1&m=2)

Unpacked, ungolfed, and commented, it looks like this.

```
3M	split into 3 approximately equal groups
M	array transpose, inserting 0 to rectangularize
"aba"]-	remove all "aba" elements from array
!	logical not; yields true iff array was empty	
```

[Run this one](https://staxlang.xyz/#c=3M%09split+into+3+approximately+equal+groups%0AM%09array+transpose,+inserting+0+to+rectangularize%0A%22aba%22]-%09remove+all+%22aba%22+elements+from+array%0A%21%09logical+not%3B+yields+true+iff+array+was+empty%09&i=%22%22%0A%22aba%22%0A%22aabbaa%22%0A%22aaabbbaaa%22%0A%22a%22%0A%22b%22%0A%22ab%22%0A%22ba%22%0A%22aab%22%0A%22aabbaab%22%0A%22aabbaaa%22%0A%22aaabbaa%22%0A%22baabbaa%22%0A%22aabbbaa%22&a=1&m=2)
Answer #7 Clojure
Anonymous 1195
# Clojure (115 bytes)
```clojure
(defn a[s](let[g(re-find #"^(a+)(b+)(a+)$"s)](or(= s"")(and(not(nil? g))(=(count(g 1))(count(g 2))(count(g 3)))))))
```

Ungolfed version:

```clojure
(defn aba?[s]
  (let [ groups  (re-find #"^(a+)(b+)(a+)$" s) ]
    (or (= s "")
        (and (not (nil? groups))
             (= (count (groups 1)) (count (groups 2)) (count (groups 3)))))))
```
Answer #8 NST
petStorm
## [NST](https://github.com/A-ee/NST), 7 bytes
I want to post in this anyway.

You need to enclose the input in brackets, e.g. `["aba"]`.
```
aba"N*∍
```
## Explanation
```
aba"N*§
aba"     Define the string "aba".
    
    N*   Multiply this string by the natural number set.
         This set is [0, 1, 2, 3, ...]
          
         Since in NST a string is technically a set,
         this yields the output
         ["", "aba", "aabbaa", "aaabbbaaa", ...]
         instead.

      §  Does the (implicit) input belong to this set?
```
Answer #9 MathGolf
petStorm
# [MathGolf], 13 bytes

    ùabam{l£3/*}=

[Try it online!][TIO-k8wzjgsf]

[MathGolf]: https://github.com/maxbergmark/mathgolf
[TIO-k8wzjgsf]: https://tio.run/##y00syUjPz0n7///wzsSkxNzqnEOLjfW1am3//1dKTExKSkxUAgA "MathGolf – Try It Online"
Answer #10 05AB1E
petStorm
# [05AB1E], 14 bytes

    …abaεIg3/иJ}JQ

[Try it online!][TIO-k8wz8hqr]

[05AB1E]: https://github.com/Adriandmen/05AB1E
[TIO-k8wz8hqr]: https://tio.run/##yy9OTMpM/f//UcOyxKTEc1s90431L@zwqvUK/P8/MTEpKTERAA "05AB1E – Try It Online"

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.