Find Strings with certain Hamming distance LINQ
<p>If we run the following (<em>thanks to @octavioccl for help</em>) LINQ Query:</p>

<pre><code>var result = stringsList
.GroupBy(s =&gt; s)
  .Where(g =&gt; g.Count() &gt; 1)        
  .OrderByDescending(g =&gt; g.Count())
  .Select(g =&gt; g.Key);
</code></pre>

<p>It gives us all the strings which occur in the list atleast twice (<em>but exactly matched i.e. Hamming Distance =0</em>). </p>

<p>I was just wondering if there is an elegant solution (<em><strong>all solutions I have tried so far either use loops and a counter which is ugly or regex</strong></em>) possible where we can specify the hamming distance in the <code>Where</code> clause to get those strings as well which lie within the specified Hamming Distance range?</p>

<p><strong>P.S: All the strings are of equal length</strong></p>

<p><strong>UPDATE</strong></p>

<p>Really thanks to krontogiannis for his detailed answer. As I mentioned earlier, I want to get list of strings with hamming distance below the given threshold. His code is working perfectly fine for it (<em>Thanks again</em>).</p>

<p>Only thing remaining is to take the strings out of the 'resultset' and insert/add into a `List'</p>

<p>Basically this is what I want:</p>

<pre><code>List&lt;string&gt; outputList = new List&lt;string&gt;();
foreach (string str in patternsList)
            {
                var rs = wordsList
    .GroupBy(w =&gt; hamming(w, str))
    .Where(h =&gt; h.Key &lt;= hammingThreshold)
    .OrderByDescending(h =&gt; h.Key)
    .Select(h =&gt; h.Count());
outputList.Add(rs); //I know it won't work but just to show what is needed
            }
</code></pre>

<p>Thanks</p>