[Question]
  [
Let's have a function \$f\$ that takes a string and removes all pairs of adjacent identical characters. For example
\$f(a\color{red}{bb}ba\color{red}{cc}) = aba\$
Note that when two pairs overlap we only remove one of them.
We will call a string perfectly paired if repeated application eventually yields the empty string. For example the string above \$abbbacc\$ is not perfectly paired because if we apply \$f\$ again we still get \$aba\$. However a string like \$eabbccadde\$ is perfectly paired because if we apply \$f\$ three times we get the empty string
\$f(ea\color{red}{bbcc}a\color{red}{dd}e) = eaae\$
\$f(e\color{red}{aa}e) = ee\$
\$f(\color{red}{ee}) =\$
---
Your task is to write perfectly paired computer code that takes a string (of printable ASCII) and decides if it is perfectly paired. **The bytestring of your source must be itself a perfectly paired string**, although your code doesn't necessarily have to be restricted to printable ASCII.
You may output two distinct values: one for the case where the input is perfectly paired, and another one for cases where it isn't.
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") question so answers will be scored by the size in bytes of their source with fewer bytes being better.
---
## Test Cases
\$abbbacc \rightarrow \mathrm{False}\\
abcba \rightarrow \mathrm{False}\\
abab \rightarrow \mathrm{False}\\
abbbaabacc \rightarrow \mathrm{True}\\
eabbccadde \rightarrow \mathrm{True}\\
bbbb \rightarrow \mathrm{True}\$
  
[Answer]
## Haskell, ~~146~~ 124 bytes
```
((""##))
a===bb=bb==a
((aa:bb))##((cc:dd))|aa===cc=bb##dd|1==1=((cc:aa:bb))##dd
a##""=""===a
""##cc=((cc!!00:cc!!00:""))##cc
```
No comments. Returns either `True` or `False`.
[Try it online!](https://tio.run/##PYzNCoNADITvPsWa9LALLehVyJO0PeRHW6nKUnv02bvdFSkMTDL5Mk9eX/00pYHcLXkPgBhCxUQkUkRcec/ciYSA6L1qZxbCxgVRzQii2dYStbRf/6xZxYgAlFVqSnV@KFBdN013GECBVdPM4@LIxfe4fNzJzRzd4K7AIsKqcM6TCu@eEz7CPm@qbNbDPX11mPixpovG@AM "Haskell – Try It Online")
Edit: -22 bytes thanks to @Cat Wizard
[Answer]
# [Python 2](https://docs.python.org/2/), 94 bytes
```
ss=''
for cc in input():ss=[cc+ss,ss[1:]][cc==ss[:1]]
print''==ss##tnirp[,+[=:)(tupni nirof=
```
[Try it online!](https://tio.run/##NY/RisQgDEXf/QrpPESpLHQfBb/E8UGjZYRZddSyO1/fsaULgdx7EzhJefdHTt87Zh/UNE17awqAkDVXikhjGlW2zrgcA404tyZa04s0ZjilhpaLMYSUGlMHOJLbradYixazVpKzvpUU6UjyqvaB@Gq9xsI4IS/1tD/OW9pkDX7DwC5fBUqNcxX1IilVT45oAoCr88STSDWk3GkJdQ3YQcC/MvrFjqe4meGexvrvIz4DXWT4C0iPyQ7WOWcRgQyFzp59JPYKw3CI1vsAHw "Python 2 – Try It Online")
The whole update step `ss=[cc+ss,ss[1:]][cc==ss[:1]]` cancels to just `=[+,[`.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~26 24 22 20~~ 18 bytes
-2 bytes thanks to [ovs](https://codegolf.stackexchange.com/users/64121/ovs). Outputs **0** if the string is perfectly paired, **1** otherwise.
```
ΔγʒgÉ}JJ}ĀqqĀÉgʒγΔ
```
[Try it online!](https://tio.run/##MzBNTDJM/f//3JRzm09NSj/cWevlVXukobDwSMPhzvRTk85tPjcFvywA "05AB1E – Try It Online")
```
ΔγʒgÉ}JJ}ĀqqĀÉgʒγΔ – Full program.
Δ       }          – Until the result no longer changes:
 γʒ  }               – Split the string into chunks of equal characters and filter by:
   gÉ                – Is the length odd?
      JJ           – And after filtering, join the parts back together, but do this
                     twice to save 2 bytes, as in the previous versions.
         Ā         – Check whether the result is empty
          q        – Terminate (quit) execution. The rest of the code is ignored.
           qĀÉgʒγΔ – Mirror the non-matched part to help with the source layout.
```
---
### Previous versions
This one purely relies on undefined behaviour (so there's no "dead code"), and outputs **[['0']]** for perfectly paired strings and **[['1']]** for non-perfectly matched strings:
```
ΔγεDgÉ£}JJ}ĀĀ£ÉgDεγΔ 
```
And the 22-byte version, explained, which is just the above but not abusing UB, and yielding *sane* values.
```
ΔγεDgÉ£}JJ}ĀqqĀ£ÉgDεγΔ – Full program.
Δ         }            – Until fixed point is reached (starting from the input value):
 γε    }                 – Group equal adjacent values, and for each chunk,
   DgÉ                     – Duplicate, get its length mod by 2.
      £                    – And get the first ^ characters of it. This yields the
                             first char of the chunk or "" respectively for odd-length
                             and even-length chunks respectively.
         JJ                – Join the result to a string, but do this twice to help
                             us with the source layout, saving 2 bytes.
            Ā           – Check if the result is an empty string.
             q          – Terminate the execution. Any other commands are ignored.
              qĀ£ÉgDεγΔ – Mirror the part of the program that isn't otherwise removed
                          anyways. This part forgoes }JJ} because that substring will
                          always be trimmed by the algorithm anyway.
```
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), 54 bytes
```
U#;[[email protected]](/cdn-cgi/l/email-protection)>??>i..??O.@1^^...u--u.u!ww;..#..U..;..;!^^!
```
Outputs nothing if the string is perfectly paired and `1` otherwise.  
[Try it here](https://ethproductions.github.io/cubix/?code=VSM7IXUxQC5PaT4/Pz5pLi4/P08uQDFeXi4uLnUtLXUudSF3dzsuLiMuLlUuLjsuLjshXl4h&input=VSM7IXUxQC5PaT4/Pz5pLi4/P08uQDFeXi4uLnUtLXUudSF3dzsuLiMuLlUuLjsuLjshXl4h&speed=60)
### Cubified
```
      U # ;
      ! u 1
      @ . O
i > ? ? > i . . ? ? O .
@ 1 ^ ^ . . . u - - u .
u ! w w ; . . # . . U .
      . ; .
      . ; !
      ^ ^ !
```
### Explanation
Most of the characters are filler needed to perfectly pair the code. Replacing those with `.` (no-op), we get
```
      U # ;
      ! u 1
      @ . O
i . ? . > i . . ? . . .
. . ^ . . . . u - . . .
. . . w ; . . . . . . .
      . ; .
      . ; !
      ^ ^ !
```
This can be broken into three steps:
* Check against the empty string (the left `i` and the `?`).
* Loop, throwing characters onto the stack and popping duplicates (everything on the bottom and right).
* Check if the stack is empty (the stuff at the top).
[Answer]
# [V](https://github.com/DJMcMayhem/V), ~~20~~, 18 bytes
```
òóˆ±òø‚
::‚øò±ˆóò
```
[Try it online!](https://tio.run/##K/v///Cmw5sPdRzaCKR3HGri4rKyOtR0eMfhTYc2Huo4vPnwpv//E5OSEpOTAQ "V – Try It Online")
Hexdump:
```
00000000: f2f3 88b1 f2f8 820a 0a3a 3a82 f8f2 b188  .........::.....
00000010: f3f2                                     ..                   ....
```
Outputs 0 for truthy, 1 for falsy. Thanks to nmjcman101 for indirectly saving 2 bytes.
```
ò        ò        " Recursively...
 ó                "   Remove...
  <0x88>          "     Any printable ASCII character
        ±         "     Followed by itself
          ø       " Count...
           <0x82> "   The number of non-empty strings
::<0x82>øò±<0x88>óò      " NOP to ensure that the code is paired
```
[Answer]
# [R](https://www.r-project.org/), ~~142~~ 126 bytes
Tighter logic and some comment bytes golfed by @Giuseppe
```
f=function(x,p="(.)\\1")"if"(grepl(p,x),f(sub(p,"",x)),!nchar(x))##x(rahcn!,x,,p(bus(f,)x,p(lperg("fi")"1\\).("=p,x(noitcnuf=f
```
[Try it online!](https://tio.run/##xc5BCoQwDIXh/dzCuPAFguABvEk3bTFakFqqhd6@U5xDzO7L5n/JremqJfonXBFV0kqY2ZiFmIIS9rylE0kqi@IurpOoXyxD9IfN6BzHimwPHwepIgmu3FDhHsOZtryDNPTcYgzPoLXHEK/w@Fj6dFOQdc57S/x5/cP037cmbl8 "R – Try It Online")
```
f=function(x,p="(.)\\1")"if"(nchar(x),"if"(grepl(p,x),f(sub(p,"",x)),0),1)##)1,)0,xp(bus(f,)x,p(lperg("fi",)x(rahcn("fi")"1).("=p,x(noitcnuf=f
```
Original:
[Try it online!](https://tio.run/##xY6xCoQwEAX7@4y18C0sYj7AP7FJgtGAxBAN@Pe5Rdvrr5tphimthSnU5K94JNySJ8LA82yIKQZC8pstuFkeW8uSd2RRDzirUyRSYxlZDHcdG@FR7gxXTwRhDWLPS1lBIZI6it18eozJ8ACaNId0xMunqistgKxz3lviz8Mv9D83/zjac/sC "R – Try It Online")
Recursive detector function followed by comment with all the characters in the function in reverse order.
[Answer]
# [Add++](https://github.com/cairdcoinheringaahing/AddPlusPlus), 146 bytes
```
D,g,@~~,L2_|*;;*|_2L,@,g,D
D,ff,@^^,BG€gBF;;FBg€GB,@D1:?:
xx:?
aa:1
`bb
Bxx;;B
Waa*bb,`yy,$ff>xx,`aa,xx|yy,`bb,Byy,xx:yy
O;;O:,B,`,|,`,>$,`,*W`
```
[Try it online!](https://tio.run/##zU47CoQwFOzfOaxkGi0T0PAIbiNY2mkSJLaWLxAs9jx7qr2I@46xxXwYZmDicVzX83iccPeNud9ra21b936G09CTR85w2wZ@fd@fkydrJz7VvhjOd2Y0RCJmJIrRdBRSIhaxlmmNsU0JoRQ0OQ8iCDFCpGqgNbCqLkshWqxdDBgBVTE0Su0ann899gM "Add++ – Try It Online")
Fun fact: This was 272 bytes long before the explanation was started, now it beats Java.
Outputs `True` for perfectly balanced strings, and `False` otherwise
To my great satisfaction, this beats the boring [palindromize version](https://tio.run/##zc6xDYNADAXQ3nNQod9AeQWcrBM0kSjpwGchaClt6USReTJVFiGMkQney9t2nvedcCBeeLVrqSlhR1zA4/f9OXggCz3l0JAosdGca4U4qr0zSIYVhyjYYcFpUiKdyIPBGSrwYsgC6/YKLtA6z2RMKtSETH0wGvh4oJGxxAdOVJe1feGKTynd/1v7AQ) by **2** bytes, to prevent the result being printed twice. I have also aimed to have as little dead code as possible, nevertheless there are still some commented-out sections, and the code exits with an error code of **1**, after printing the correct value.
*NB* : A bug with the `BF` commands was [fixed](https://github.com/cairdcoinheringaahing/AddPlusPlus/commit/17d68bf59505bcef7058c2e7319d3c897447883a) while this answer was in development.
## How it works
The code starts by defining the two key functions, \$\mathit{ff}\$ and \$g\$. These two functions are used to calculate the next step in the process of removing pairs, and work entirely from \$\mathit{ff}\$ i.e. only \$\mathit{ff}\$ is called from the main program, never \$g\$. If we define the input string as \$S\$, \$\mathit{ff}(S)\$ modifies \$S\$ in the following way:
First, identical adjacent characters in \$S\$ are grouped together. For an example of \$abbbaabacc\$, this yields the array \$[[a], [bbb], [aa], [b], [a], [cc]]\$. Over each of the sublists (i.e. the identical groups), we run the function \$g\$, and replace the sublists with the result of the function.
\$g\$ starts by unpacking the group, splatting the characters onto the stack. It then pushes the number of characters on the stack and takes the absolute difference with \$2\$ and that number. We'll call this difference \$x\$. Lets see how this transforms the respective inputs of \$[a]\$, \$[bb]\$ and \$[ccc]\$:
$$[a] \Rightarrow [a, 1]$$
$$[bb] \Rightarrow [b, b, 0]$$
$$[ccc] \Rightarrow [c, c, c, 1]$$
As you can see \$x\$ indicates how many of the next character we wish to keep. For simple pairs, we remove them entirely (yielding **0** of the next character), for lone characters we leave them untouched, or yield **1** of them, and for groups where \$x > 2\$, we want \$x - 2\$ of the character. In order to generate \$x\$ of the character, we repeat the character with `*`, and the function naturally returns the top element of the stack: the repeated string.
After \$g(s)\$ has been mapped over each group \$s\$, we splat the array to the stack to get each individual result with `BF`. Finally, the `^` flag at the function definition (`D,ff,@^^,`) tells the return function to concatenate the strings in the stack and return them as a single string. For pairs, which yielded the empty string from \$g\$, this essentially removes them, as the empty string concatenated with any string \$r\$ results in \$r\$. Anything after the two `;;` is a comment, and is thus ignored.
The first two lines define the two functions, \$\mathit{ff}\$ and \$g\$, but don't execute \$\mathit{ff}\$ just yet. We then take input and store it in the first of our 4 variables. Those variables are:
* \$\mathit{xx}\$ : The initial input and previous result of applying \$\mathit{ff}\$
* \$\mathit{yy}\$ : The current result of applying \$\mathit{ff}\$
* \$\mathit{aa}\$ : The loop condition
* \$\mathit{bb}\$ : Whether \$\mathit{yy}\$ is truthy
As you can see, all variables and functions (aside from \$g\$) have two letter names, which allows them to be removed from the source code fairly quickly, rather than having a comment with a significant amount of \$\mathit{xyab}\$. \$g\$ doesn't do this for one main reason:
If an operator, such as `€`, is run over a user defined function \$\mathit{abc}\$, the function name needs to be enclosed in `{...}`, so that the entire name is taken by the operator. If however, the name is a single character, such as \$g\$, the `{...}` can be omitted. In this case, if the function name was \$\mathit{gg}\$, the code for \$\mathit{ff}\$ and \$g\$ would have to change to
```
D,gg,@~~,L2_|*;;*|_2L,@D             (NB: -2 bytes)
D,ff,@^^,BG€{gg}BF;;FB}gg{€GB,@D?:   (NB: +6 bytes)
```
which is 4 bytes longer.
An important term to introduce now is the *active* variable. All commands except assignment assign their new value to the active variable and if the active variable is being operated on, it can be omitted from function arguments. For example, if the active variable is \$x = 5\$, then we can set \$x = 15\$ by
```
x+10 ; Explicit argument
+10  ; Implicit argument, as x is active
```
The active variable is \$x\$ by default, but that can be changed with the ``` command. When changing the active variable, it is important to note that the new active variable doesn't have to exist beforehand, and is automatically assigned as 0.
So, after defining \$\mathit{ff}\$ and \$g\$, we assign the input to \$\mathit{xx}\$ with `xx:?`. We then need to manipulate our loop conditions ever so slightly. First, we want to make sure that we enter the while loop, unless \$\mathit{xx}\$ is empty. Therefore, we assign a truthy value to \$\mathit{aa}\$ with `aa:1`, the shortest such value being \$1\$. We then assign the truthiness of \$\mathit{xx}\$ to \$\mathit{bb}\$ with the two lines
```
`bb
Bxx
```
Which first makes \$\mathit{bb}\$ the active variable, then runs the boolean command on \$\mathit{xx}\$. The respective choices of \$\mathit{aa} := 1\$ and \$\mathit{bb} := \neg \neg \mathit{xx}\$ matter, as will be shown later on.
Then we enter our while loop:
```
Waa*bb,`yy,$ff>xx,`aa,xx|yy,`bb,Byy,xx:yy
```
A while loop is a *construct* in Add++: it operates directly on code, rather than variables. Constructs take a series of code statements, separated with `,` which they operate on. While and if statements also take a condition directly before the first `,` which consist of a single valid statement, such as an infix command with variables. One thing to note: the active variable cannot be omitted from the condition.
The while loop here consists of the condition `aa*bb`. This means to loop while both \$\mathit{aa}\$ and \$\mathit{bb}\$ are truthy. The body of the code first makes \$\mathit{yy}\$ the active variable, in order to store the result of \$\mathit{ff}(x)\$. This is done with
```
`yy,$ff>xx
```
We then activate our loop condition \$\mathit{aa}\$. We have two conditions for continued looping:
* 1) The new value doesn't equal the old value (loop while unique)
* 2) The new value isn't the empty string
One of Add++'s biggest drawbacks is the lack of compound statements, which necessitates having a second loop variable. We assign our two variables:
$$\mathit{aa} := \mathit{xx} \neq \mathit{yy}$$
$$\mathit{bb} := \neg \neg (\mathit{yy})$$
With the code
```
`aa,xx|yy,`bb,Byy
```
Where `|` is the [inequality](https://github.com/cairdcoinheringaahing/AddPlusPlus/blob/master/add%2B%2B.py#L2444) operator, and `B` [converts to boolean](https://github.com/cairdcoinheringaahing/AddPlusPlus/blob/master/add%2B%2B.py#L2453). We then update the \$\mathit{xx}\$ variable to be the \$\mathit{yy}\$ variable with `xx:yy`, in preperation for the next loop.
This while loop eventually reduces the input into one of two states: the empty string or a constant string, even when applied to \$\mathit{ff}\$. When this happens, either \$\mathit{aa}\$ or \$\mathit{bb}\$ result in False, breaking out of the loop.
After the loop is broken, it can break for one of two reasons, as stated above. We then output the value of \$\mathit{aa}\$. If the loop was broken due to \$\mathit{x} = \mathit{y}\$, then both the output and \$\mathit{aa}\$ are False. If the loop was broken because \$\mathit{yy}\$ was equal to the empty string, then \$\mathit{bb}\$ is falsy and \$\mathit{aa}\$ and the output are truthy.
We then reach our final statement:
```
O
```
The program can now be in one of three states, in all of which the active variable is \$\mathit{bb}\$:
* 1) The input was empty. In this case, the loop didn't run, \$\mathit{aa} = 1\$ and \$\mathit{bb} = \mathrm{False}\$. The correct output is \$\mathrm{False}\$.
* 2) The input was perfectly balanced. If so, the loop ran, \$\mathit{aa} = \mathrm{True}\$ and \$\mathit{bb} = \mathrm{False}\$. The correct output is \$\mathrm{False}\$
* 3) The input was not perfectly balanced. If so, the loop ran, \$\mathit{aa} = \mathrm{False}\$ and \$\mathit{bb} = \mathrm{True}\$. The correct output is \$\mathrm{True}\$
As you can see, \$\mathit{bb}\$ is equal to the expected output (albeit reversed from the *logical* answer), so we simply output it. The final bytes that help us beat Java come from the fact that \$\mathit{bb}\$ is the active variable, so can be omitted from the argument, leaving us to output either \$\mathrm{True}\$ or \$\mathrm{False}\$, depending on whether the input is perfectly balanced or not.
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 38 bytes
```
''≡{'(.)\1'⎕R''⊢⍵}⍣≡⍝⍝≡⍣}⍵⊢R⎕'1\).('{≡
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R2wR19UedC6vVNfQ0YwzVH/VNDQIKdC161Lu19lHvYqDUo965IARiLAYKbQVKBgGVqRvGaOppqFcDJYDmKKgnJiUlJSYnq3OB2clJiVAWUDQRLpEK5CcnJ6akpIK5IAC2HGo71HoiHQB1AdgJ6gA "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), ~~28~~ 26 bytes
```
+`(.)\1
C`^$
$^`C1\).(`+
```
[Try it online!](https://tio.run/##K0otycxLNPyvquGe8F87QUNPM8aQi8s5IU6Fi0slLsHZMEZTTyNB@///xKSkpMTkZK7EpOSkRC4wLxEskApkJycnpqSkckH1H9p2aBvIBBCNZAYA "Retina – Try It Online")
Outputs ``C1\).(`+0`C1\).(`+` for falsy and ``C1\).(`+1`C1\).(`+` for truthy cases.
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), ~~228~~ 200 bytes
```
(()){{{}([]<<{{(({}<<>>)<<>>[({})]){{{{}(<<<<>>()>>)((<<>>))}}}{}{}<<>>{}<<>>}}{}<<>>>>[[]])}}{}(<<({{(())(<<()>>)}}<<>>)>>){{{{}{}}((){{}{}{}{}{}}(()())())[[]((){{}{}}())[]]((){{}{}}[[][]]()){{}{}}}}
```
[Try it online!](https://tio.run/##3Y9BCsMwDAS/oz3kByIfMTq4h0JpyCFXsW9PtHZK/xBshMa7WuHX0T/78t769zzNgMyktXDPNEu6rytUWgFCcunuejKUZjYsIJmc/ll5U422CAjLaooF1Gmac0HdEZxkqaOZRyg7UCE/icL4Y0lC3Eg@6CdLvwA "Brain-Flak – Try It Online")
This is a bit of a proof of concept. It could probably be shorter. It doesn't use any comments however.
Outputs `0,0` if the input is perfectly paired and `0,1` if the input is not. 
[Answer]
# [sed 4.2.2](https://www.gnu.org/software/sed/), 34 bytes
```
:;:t;ss((..??\??))\1ss1;t;/..??/cc
```
[Try it online!](https://tio.run/##hYwxEoAgDMB2XgKDcKx06EdY2sLgIp7l/VblA25JhmhvZgXKBFXvY0SsiCHUrJphQvpKEjEjZiYRRyxMbhmt0F8Woda6@//c45z7ONS26wE "sed 4.2.2 – Try It Online")
Paired strings give empty output, unpaired ones give `ct:`
The trivial palindromic version is at 32 `:;ss(.)\1ss;t;/./cc/./;t;1\).(;:`. Old solution was `:;ss((..??\??))\1ss1;t;;/./cc/./t:` (changed because current one abused `c` less, edit: yay now there's only 1 character after `c` :D)
(note that `;` is the statement separator)
`:` declares an empty label
`:t` declares the label `t`
`ss((..??\??))\1ss1` is a substitution, in sed you can change the delimiter to a substitution, and this is what I did by changing it to `s`, so what this does is substitute the first (as is denoted by the `1` at the end)
* match of `((..??\??))\1`
	+ `.` any character
	+ `.??` followed by an optional optional character
	+ `\??` and an optional `?`
	+ followed by the same thing right beside it
* with nothing
Now this substitution is paired with itself, so the `;`s before and after it get cancelled out too
`t` and loop back to the label until there are no more successful substitutions
`/..?/` if `.` (wildcard) followed by `.?` an optional character is matched
* `cc` change the buffer to `c`
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), ~~112 110~~ 108 bytes
```
(()){{}({<<(({}<<>>)<<>>[({})]){{((<<>>)<<>>)}}{}{##{
}<<>>>>{}<<>>}<<>>)}{}((){{<<>>[[]]}})##}{}])}{([)}(}
```
[Try it online!](https://tio.run/##vY2xDYAwDAR71kjzLrJBlEWiFKFAQiAK2tfPHkwiMQKNJf/5/Ovd9ituZzt6B8xIgSkBVEo52zuKL1YdAV9mEsUQuCzjMOcpTMuZPyOHXWqVLAQPqxMUE/RzW2wP "Brain-Flak – Try It Online")
This is based on [my answer](https://codegolf.stackexchange.com/a/163534/76162) from *[Are the brackets matched?](https://codegolf.stackexchange.com/q/77138/76162)*.
Tried not to use comments, but got stuck trying to make the pop nilads (`{}`) pair up. The problem lies in the easiest way to pair up a pair of brackets is to surround it in another pair of the same kind. While this is easy for other nilads, the `{...}` monad creates loops. In order to exit the loop you have to push a 0, but once you've exited the loop, you then have to pop the 0, which compounds the problem.
The 66 byte pre-paired solution is:
```
(()){{}({<(({}<>)<>[({})]){((<>)<>)}{}{}<>>{}<>}<>)}{}((){<>[[]]})
```
[Try it online!](https://tio.run/##TYwxCsUwDEMP08UaeoOQi4QM6VAov3ToKnT21E4I/EW2Zekdb7ue/bzbr3czgJQxmVEpI@XiCypoNk6Iik8O0by9RQ@WWoV/xoR4FiELFKTlQRPn6yD6nIVFjd7gbpt7YSMKdJBoBTL1vX0 "Brain-Flak – Try It Online")
Outputs `1` or `1,0` if the input is a perfect pairing, `0,0` if not.
### No comment version, 156 bytes
```
(()){{{}({<<(({}<<>>)<<>>[({{}((<<[[]]>>)){}}{}(<<[]>>){{}{}}{})]){{((<<>>)<<>>)}}{{}{}{}}{}{}<<>>>>{}<<>>}<<>>)}}{{}{}}{}((){<<>>[[]]})(<<()()>>){{}{}{}}{}
```
[Try it online!](https://tio.run/##zY49DoAgDIWv897ADRouQhhwMDEaB9emZ8cWNPEILsD76VeWq21nWo@29w6QqmpQEUBNJGfGURAuREqp1T2qmRuuQ3k2NKs/o/VM0c2IRjhpOc/bvnmgQB2LnG90BAi@6NH4@e9SuwE "Brain-Flak – Try It Online")
As Cat Wizard pointed out, the first answer does not work for all interpreters, as not all handle `#` comments. This version contains no comments.
[Answer]
# Japt, ~~24~~ 22 bytes
Outputs `false` for truthy and `true` for falsey.
```
&&!!e"(.)%1"PP"1%).("e
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.5&code=JiYhIWUiKC4pJTEiUFAiMSUpLigiZQ==&input=WwoiYWJiYmFjYyIKImFiY2JhIgoiYWJiYmFhYmFjYyIKImVhYmJjY2FkZGUiCiJiYmJiIgonJiYhIWUiKC4pJTEiUFAiMSUpLigiZScKXS1tUg==)
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 96 bytes
```
{<<>>(({})<<>>[(([{}<<>>]))]){((<<[[]]>>))}{}{}{}{}<<>>}<<>>{{{}{}{}{}{}}((<<>>){{}{}}){{{}}}}{}
```
[Try it online!](https://tio.run/##vU0xCoBADPtOM/iD0o@UDucgiOLgGvr283qITzCBEJJA1rvt17Kd7eidqmYiTJRxEWeWCyBAEVX3CDMg@bLqKSS/MGs6ZjNKVDXA/OFhaQ8 "Brain-Flak – Try It Online")
Outputs nothing if the input is perfectly paired, and `0` otherwise.
Non-perfectly paired (original) version:
```
{<>(({})<>[({}<>)]){((<()>))}{}{}{}<>}<>{((<>))}{}
```
[Try it online!](https://tio.run/##RU07CsAwCL2Ob8gNxItIhnQolJYOXcWzp5qEVkXlfXR72nGX/Wpn78ZCZA4WjcGCCiNiggBuI1miEpxQeliWKxYl0hDFVrHcrFqr/BcmPZqZfaCnNGQDciQVER9KewE "Brain-Flak – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 66 bytes
```
null.foldr((%))"";;cc%((r:t))|cc==r=t;;t%s=t:s--s:t=s%=r|t:dlof.un
```
[Try it online!](https://tio.run/##lY5BDsIgEEWvQogkkNgeADInsS6GoWgjpQTormcvEuPKnav/31u9J5bXHELzMLW4hzD6LbgspVCKc2OIhJRZV6UOIoAM1ZgqClRdhqHoCkVAPqp2YfPjHtuKS4SUl1jZha2YmGc3jtZaJOLX/sjiZ7vBr5w7EaFzc4efholP/1fwezvJB3yUNlBKbw "Haskell – Try It Online")
Cat Wizard saved 6 bytes.
[Answer]
# JavaScript (ES6), 76 bytes
Returns a boolean.
```
ff=ss=>ss==(ss=ss.replace(/(.)\1/,''))?!ss:ff(ss)//)(:!?,/1\).(/(ecalper.=(>
```
[Try it online!](https://tio.run/##hc/BCsIwDAbgu08xd1kLs2XXQbebT@BxlzRLRSnraNTXrzkoCIMZSE4ff/jv8ALGfFsfpyXNVEoIjtkNsk7JYTaZ1ghIyiqjp862TaP1eGTuQxChrdWqP46t7SZtBBFCXCkbp4aCaeEUycR0VaJr8N4DYq11JWNtdYbIdNgy9PBBu0zS4Bso7JKfW0XCEGGeaU/9qb3fe1P851N5Aw "JavaScript (Node.js) – Try It Online")
Suggested by @Shaggy: [**58 bytes**](https://tio.run/##fZC9DsIwDIR3nsIwlEQqiZhRO/IEjCyu6/CjiFZxQEKIZy8pIP6FBw/@7s7SbfGAQmHTxsmuqbnrnCtEilIky5xTIiZw65FYWWX0cmrz8Vhra3M7XWqTjkzoWw5GlV1kiVCAQFHCCWI4pj3sQzRkGVCzk8az8c1KxbBnPYMzEEZaK9ZJ@coderkKzoNBn6pGWFUVEo00pLEW5r3iwajCG/nBkg/v1sQW6fMdcWJEWNf8hf6X8N7CZw3PsO4C) by returning an empty string for *perfectly paired* or throwing an error otherwise.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~70~~ 64 bytes
```
{#//.{aa___,bb__,bb_,cc___}:>{aa,cc}}=={{}}(**(,{>:}_,{.#{&)**)&
```
[Try it online!](https://tio.run/##pY1BCsIwFET3OUYCpQ2x3VdaAl5A1yrh5zelXbSLGFeff/YYUPQAboaZNzCzQVrCBmlFyPNQZ1Jd1xKAc854/xaDWCL3Y@HFMw8DEXOtdW1o7NkZahVVjdZNlZujEJfnGpI9x3VPV3UYZ3taIAKmEB9WGXnb5b3qLAkh/72TpoyA9x4QPx49/Ch8i1AyIkxTkEJwfgE "Wolfram Language (Mathematica) – Try It Online")
### Without comments, 92 bytes
```
((#//.bb_:>StringReplace[00[ecalpeRgnirtS>aa__:_.#&];bb,{{,}};00>00;00;aa:_~~aa_:>""]))==""&
```
[Try it online!](https://tio.run/##rY5BCoMwEEX3OcYIomA164jBIxRdaggzMbaCikh2ole3KZX2At0M/8185v8J3dNO6AaDZ19EZxQFWZYSaSFrtw7zo7LLiMY2nDfW4LjY6jEPq6slotZCp0GocqJk25J9zzmXnPuZIwp9HN4iJICK46IACM84Z@zun7oGGCgPFwU32ZdBAu0MKszKjTH4f48W2k@Ttwgh8SFIRGjMpQ3hb4vfg/VsDHadBcb28wU "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Lua](https://www.lua.org), 178 bytes
```
p=...S={}for a in p:gmatch"."do E=S[#S]~=a;S[E and#S+1 or#S]=E and a or X end;print(#S==0)--)0S#(tnirp;dne X ro a dna E=]S#ro 1+S#dna E[S;a=~]S#[S=E od"."hctamg:p ni a rof}{=S.=p
```
[Try it online!](https://tio.run/##1Y49CoQwEIWvMiSNIhu0NUzpCaZZCBbB@AdrEkK2Er16dvAWW36P972Zz9eWElEpRXheS0hgYfcQ@/WwedqEEi7AgGQkjTdaTWYA652kpoOQOMSH2WL1DbN3Oqbd50oSYlu/XnVLssp@T1E7P3MlBS47b3l1JMnUNSQfNqQt3hwa4tXg@Pg2ZXusfQS/s5XCcp1ICmMpRfzf1@IH "Lua – Try It Online")
While it is a terribly long solution, this does make quite a bit of use of Lua-specific quirks. This is actually a minified brute force stack algorithm. The program is made complicated by the fact that Lua's patterns don't allow replacing pairs and regex is not built in.
Explanation:
```
p=... -- command-line argument
S={} -- the stack
for c in p:gmatch"." do -- shorter than "for i=1,#p do ..."
    E=S[#S]~=c -- check whether we have the right letter on top of stack
    -- could've saved some bytes by doing == instead of ~=
    -- but the double negation is necessary for ternary operator
    -- to work with nil values
    S[E and #S+1 or #S]=E and c or X -- Lua's awesome "ternary operator"
end
-- i'm sure there is a better way to output this (table indexing?)
print(#S==0)
```
[Answer]
# [Gol><>](https://github.com/Sp3000/Golfish), 30 bytes
```
1ll1**F:}}:{=Q{~~||lzBBzl{Q={F
```
[Try it online!](https://tio.run/##S8/PScsszvhv6JiW6Zpm5ZpR998wJ8dQS8vNqrbWqto2sLqurqYmp8rJqSqnOtC22u0/AXkA "Gol><> – Try It Online")
Everything after the first `B` is excess code and is not executed. A function that returns the top of stack as `1` if the input is a perfect pairing, `0` otherwise.
### Explanation:
```
1       Push 1 as the end string marker
 ll1**  Push n, where n (len+1)*(len+2), 
        This is larger than the amount of steps needed to determine pairing
      F           |  Repeat that many times
       :}}:{=        Compare the first two characters of the string
             Q   |   If they are equal
              {~~    Pop both of them
        String is also rotated by 1
        If the string becomes empty, the 1 is compared to itself and removed.
                   lzB   Return whether the length of the stack is 0
                      Bzl{Q={F  Excess code to match unpaired symbols
```
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), 30 bytes
```
1O@;??;@ii??O;>>;;;..1Wcc1??1W
```
[Try it online!](https://tio.run/##Sy5Nyqz4/9/Q38Ha3t7aITPT3t7f2s7O2tpaT88wPDnZ0N7eMJyQPAA "Cubix – Try It Online")
Outputs `1` if the string is perfectly paired and nothing otherwise.
### Cubified
```
      1 O @
      ; ? ?
      ; @ i
i ? ? O ; > > ; ; ; . .
1 W c c 1 ? ? 1 W . . .
. . . . . . . . . . . .
      . . .
      . . .
      . . .
```
### Simplified
```
      1 O @
      ; ? .
      . @ .
i ? . . . . > ; ; ; . .
. W c . . . ? 1 W . . .
. . . . . . . . . . . .
      . . .
      . . .
      . . .
```
The logic and general structure are the same as in Mnemonic's answer, but without an explicit check for the empty string.
[Answer]
# [Haskell](https://www.haskell.org/), 92 bytes
```
gg""
gg((aa:cc:bb))dd|aa==cc=gg bb dd
gg  aa""=""==aa
gg  aa((bb:c))=gg((bb:aa))c--c:=c:|
```
[Try it online!](https://tio.run/##NYwxDsMgEAR7vwKhFFDwAaSr8gGXKdLsHeRihVgxUPrtIXYRaYvRaLRPtFcuZWx0H6rWTpOqc0AUiczep7QDRCKkaphNSmdhDGAtHSPgL5xjjuI9nQ8HAt5LCBJJ4j7eWFb61GXtl8323HpvteZux1ceBdpGuF3n@Qc "Haskell – Try It Online")
[@nimi's answer](https://codegolf.stackexchange.com/a/167436/56656) is pretty cool, it doesn't use any comments. This one is shorter but does use a comment.
[@xnor's answer](https://codegolf.stackexchange.com/a/167455/56656) is also pretty cool, it does use comments and is shorter than this one.
[Answer]
# [Python 2](https://docs.python.org/2/), 114 bytes
```
import re
e=lambda i,nn=1:e(*re.subn('(.)\\1','',i))if nn else''==i##ieslef'1).('(nbus.er*(e:1=,i adbmal=r tropmi
```
[Try it online!](https://tio.run/##Xc5BasQwDAXQfU4hmILsIRjS5RSveo1sZFuhAscxsgc6p0890MW028fX/6qP/nWU91P2emgH5Skeib0i4otN7DPtIRHIXIpfbmyuyq7dQzFonF3XBWfEWayVDUoBzo0RvZfLRbhl3nCxbkRLuDfHejV8W/wsQCnslL1C16Puco5Z17pKNfYD@JsjPN@ZpqpSOrBBCiFQjGhfKQb6CyND/2M8OEZKiYf@Mn6OdpAGlXXj2PMDKolyuuE8bp7bTrlmiqNgLQPxDa09fwA "Python 2 – Try It Online")
Returns `True` for perfectly-paired strings, `False` otherwise.
(Actually fails to verify itself, because `(.)` won't match the newlines in the code! But @Cat Wizard said this is okay, because newlines aren't printable ASCII characters, so my program needn't handle them.)
---
This is a perfectly-paired version of:
```
import re;p=lambda s,n=1:p(*re.subn('(.)\\1','',s))if n else''==i
```
for which a “lazier” perfectization of `code + '##' + f(code[::-1])` would give 120 bytes. (That is, renaming the variables etc. to introduce more collapsed pairs inside the *comment* half of the code saved 6 bytes.)
---
> 
> `re.subn` is a little-known variant of `re.sub` that returns a tuple `(new_string, number_of_substitutions_made)`. It's pretty good for finding regex substitution fixpoints!
> 
> 
> 
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~26 24~~ 22 bytes
```
ẠƬµF€ḂLḣgŒŒgḣLḂ$$€FµƬẠ
```
[Try it online!](https://tio.run/##y0rNyan8///hrgXH1hza6vaoac3DHU0@D3csTj866eikdCADyGlSUQFKuB3aemwNUOH/h7u3HG4HCvz/r6urFs8VFeEcxpWYlJSUmJwMpJOTEiG8RLBAKpCdnJyYkpLKRYolXIZGxiamZrq6ZqYmxkaGAA "Jelly – Try It Online")
Weirdly seems to work without moving the backwards code to an unused link.
Returns **0** if the input is perfectly paired, **1** otherwise.
Active code:
```
ŒgḣLḂ$$€FµƬẠ
Œg            Group runs 'abbbcc'->['a','bbb','cc']
       €      For each of these strings:
      $       Monad{
     $            Monad{
   L                  Find the length...
    Ḃ                 ...mod 2. 
                      } -> [1, 1, 0] in this example.
  ḣ               Take this many characters from the string.
                  } -> [['a'], ['b'], []]
        F     Flatten -> ['a', 'b']
          Ƭ   Repeat...
         µ    The last monadic chain until a fixed point is reached.
           Ạ  All. If it is not a perfectly paired string, all elements in the 
              result of Ƭ will be nonempty and 1 is returned.
              If it is perfectly paired, the last element is [] which is falsy
              and 0 is returned.
```
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/Attache), 82 bytes
```
{0=#Fixpoint[{Replace[_,/"(.)\\1",""]},_]}??}]_,}],"1).("/,_[ecalpeR{[tniopxiF#=0{
```
[Try it online!](https://tio.run/##LU49i4NAEO39FcvGIoHBxPZAUgSsjnDIdZNBxtkxtyC6xC0E8bd7m@Oq98V7PI6R5Uf33nxU@3qpDrVfwuTHiGujYWBRbOFsj8Xp8SgtWEsbtLRdrxu1sBHY8lQc7RlaVOEhaLNiHP0UFl8fqsu6y@Q0TZtPP@qMtR@0UXZoi3dQRD9ZIiwpy@6qbsY8BHlS9vVKD751jjeeU60Hgzl3XcciYBKTjv8wOfxvalIi7JyCeW8T7b8 "Attache – Try It Online")
Nothing incredible here. `Fixpoint`s a function which removes consecutive pairs.
[Answer]
# Java 8, ~~158~~ ~~156~~ 154 bytes
```
n->{for(;n.matches(".*(.)\\1.*");n=n.replaceAll("(.)\\1",""));return  n.isEmpty();}//};)(ytpmEsi.ruter;,"1).("(Aecalper.n=n;)"*.1).(*."(sehctam.n;(rof{>-n
```
Returns a boolean (`true`/`false`).
-2 bytes thanks to *@raznagul*.
[Try it online.](https://tio.run/##lVKxbsMgFNzzFU9MYCVYWYtSKUO6NUu6lQ7PmDROMbYAR7Iif7tLHKdDG7mKBAO8494d7454wsUx/@qVQe/hFQt7ngEUNmi3R6VhezkCZFVlNFpQdBdcYT/BMxELXdxx@YChULAFC6veLp7P@8pRYXmJQR20p4QnlDMplzwhTNiV5U7XJtKvjaHkWiJzQhgTTofGWQDLC78p69BSJro07QSjbajLjS@4a6I4MSdLxuPjtVZoau14pBWMJPxynXBCvT6ogCW3grpqf35e2F5c5dZNZqLcUfWpKnIoo/HR2vsHILu6TlN4i90O7dNw3LU@6JJXTeB1RAZjqeWKEo1ZphTmub44mIRGZIaYoVJT0LFy0/CCxut/NAzE06wjTmX4QO9d1bgYA1XlGorgtdlP67gzfXkb/xAA@TcBktzKkswlkY/lQA5BkL@TIH@iIO9lYfyCbtb13w)
**Explanation:**
```
n->{                              // Method with String parameter and boolean return-type
  for(;n.matches(".*(.)\\1.*");   //  Loop as long as the String still contains pairs
    n=n.replaceAll("(.)\\1","")); //   Remove all pairs
  return  n.isEmpty();}           //  Return whether the String is empty now
//};)(ytpmEsi.ruter;,"1).("(Aecalper.n=n;)"*.1).(*."(sehctam.n;(rof{>-n
                                  // Comment reversed of the source code,
                                  // minus the pairs: '\\';'ll';'\\';'""))';'n  n';'//'
```
]