title: Count number of character matches in a string (Regex only)?
body: <p>So I recently had an interview question asking me to determine if a string was balanced or not. A string is balanced under these two conditions:</p>
  
  <ol>
  <li>The sum of the occurrences of 'a' and 'c' is even.</li>
  <li>The sum of the occurrences of 'b' and 'd' is even.</li>
  </ol>
  
  <p>So given the example "<code>cccddbba</code>" these conditions hold true. Occurrences of <code>c (3)</code> + occurrences of <code>a (1)</code> is <code>even (1+3=4)</code> and occurrences of <code>d (2)</code> + occurrences of <code>b (2)</code> is <code>even (2+2=4)</code>. The strings can only contain the characters 'a', 'b', 'c', and 'd'. The regex should work for any string, not just this example.</p>
  
  <p>My task was only to write the regex for this. I assume in the background they count the number of matches of 'a' and 'c' found using my regex and sum them up. The same goes for 'b' and 'd'.</p>
  
  <p>I haven't touched regexes in a while, so I did horrible and only got as far as <code>^([ac])*?[db]*?([ac])*?$</code>, which is obviously incorrect.</p>