title: find matching characters in two strings at different indexes
body: <p>I am a C++ programmer. Out of interest, I am developing a java application. </p>
  
  <p>I have two strings in java:</p>
  
  <pre><code>String word1 = "Fold";
  String word2 = "Flow";
  </code></pre>
  
  <p>Now I need a function to get the count of matching characters in both strings but those that are at different indexes. The strings could be of any length but always both words will be of the same length.</p>
  
  <p>Added:
  We should increment count for a character by that many occurrences in both words. Ex: abcd and xyaa should return 1, but abca and xaay should return 2. Hope it is clear now.</p>
  
  <p>For ex:, the count for the above example should be 2 (Only letters 'o' and 'l' are considered. Though letter 'f' is present in both words, it is not considered since it is present at the same index on both strings.</p>
  
  <p>My method was to create two map variables Map and initialize it with 0 for all characters. Then calculate count of how many times each letter occurs in both strings and finally check how many of these characters have count more than one. </p>
  
  <p>Ex:</p>
  
  <pre><code>    Map&lt;Character, Integer&gt; word_count_1 = createMap(); // initialize with a:0, b:0, c:0,...z:0
      Map&lt;Character, Integer&gt; word_count_2 = createMap(); // initialize with a:0, b:0, c:0,...z:0
  
      int count, value;
  
      for (int i=0; i&lt;word1.length(); i++)
      {
          if (word1.charAt(i) != word2.charAt(i))
          {
              value = word_count_1.get(word1.charAt(i));
              word_count_1.put(word1.charAt(i), ++value);
  
              value= word_count_2.get(word2.charAt(i));
              word_count_2.put(word2.charAt(i), ++value);
          }
      }
  
      Set set = word_count_2.entrySet();
      Iterator i = set.iterator();
      Map.Entry&lt;Character, Integer&gt; iter;
  
      while(i.hasNext())
      {
          iter = (Map.Entry)i.next();
          if ( (iter.getValue() &gt; 0) &amp;&amp; (word_count_1.get(iter.getKey())) &gt; 0 )
          {
              count++; // This line has a bug. We shall ignore it for now
          }
      }
  </code></pre>
  
  <p>Is there any other better method to get the count instead of what I am trying to do? I just dont get a good feeling about what I have done.</p>
  
  <p>Edited:</p>
  
  <p>The line count++ (that I mentioned having a bug) should be changed to following to give correct result:</p>
  
  <pre><code>int letterCount1 = word_count_1.get(iter.getKey());
  int letterCount2 = iter.getValue();
  if ( (letterCount1 &gt; 0) &amp;&amp; (letterCount2 &gt; 0 )
  {
      int minVal = letterCount1;
      if (minVal &gt; letterCount2)
          minVal = letterCount2;
      count+= minVal;
  }
  </code></pre>