title: Reduce a string using grammar-like rules
body: <p>I'm trying to find a suitable DP algorithm for simplifying a string. For example I have a string <code>a b a b</code> and a list of rules</p>
  
  <ol>
  <li><code>a b -&gt; b</code></li>
  <li><code>a b -&gt; c</code></li>
  <li><code>b a -&gt; a</code></li>
  <li><code>c c -&gt; b</code></li>
  </ol>
  
  <p>The purpose is to get all single chars that can be received from the given string using these rules. For this example it will be <code>b, c</code>. The length of the given string can be up to 200 symbols. Could you please prompt an effective algorithm?</p>
  
  <p>Rules always are <code>2 -&gt; 1</code>. I've got an idea of creating a tree, root is given string and each child is a string after one transform, but I'm not sure if it's the best way.</p>