title: Finding minimum moves required for making 2 strings equal
body: <p>This is a question from one of the online coding challenge (which has completed).<br>
  I just need some logic for this as to how to approach.</p>
  
  <p><strong>Problem Statement:</strong><br>
  We have two strings A and B with the same super set of characters. We need to change these strings to obtain two equal strings. In each move we can perform one of the following operations:</p>
  
  <pre><code>1. swap two consecutive characters of a string  
  2. swap the first and the last characters of a string
  </code></pre>
  
  <p>A move can be performed on either string.<br>
  What is the <strong>minimum</strong> number of moves that we need in order to obtain two equal strings?  </p>
  
  <p><strong>Input Format and Constraints:</strong><br>
  The first and the second line of the input contains two strings A and B. It is guaranteed that the superset their characters are equal.  </p>
  
  <pre><code>1 &lt;= length(A) = length(B) &lt;= 2000
  All the input characters are between 'a' and 'z'
  </code></pre>
  
  <p><br />
  <strong>Output Format:</strong><br>
  Print the <strong>minimum</strong> number of moves to the only line of the output</p>
  
  <pre><code>Sample input:
  aab
  baa
  
  Sample output:
  1
  </code></pre>
  
  <p><strong>Explanation:</strong><br>
  Swap the first and last character of the string aab to convert it to baa. The two strings are now equal.</p>
  
  <p><strong>EDIT :</strong> Here is my first try, but I'm getting wrong output. Can someone guide me what is wrong in my approach.</p>
  
  <pre class="lang-c prettyprint-override"><code>int minStringMoves(char* a, char* b) {
      int length, pos, i, j, moves=0;
      char *ptr;
      length = strlen(a);
  
      for(i=0;i&lt;length;i++) {
          // Find the first occurrence of b[i] in a
          ptr = strchr(a,b[i]);
          pos = ptr - a;
  
          // If its the last element, swap with the first
          if(i==0 &amp;&amp; pos == length-1) {
              swap(&amp;a[0], &amp;a[length-1]);
              moves++;
          }
          // Else swap from current index till pos
          else {
              for(j=pos;j&gt;i;j--) {
                  swap(&amp;a[j],&amp;a[j-1]);
                  moves++;
              }
          }
  
          // If equal, break
          if(strcmp(a,b) == 0)
              break;       
     }
  
     return moves;
  }
  </code></pre>