Note that there are some explanatory texts on larger screens.

plurals
  1. POGet number of different chars between two strings allowing a defined number of differences
    primarykey
    data
    text
    <p>I want a method that receives as input <code>two char arrays</code>, and an integer <code>d</code> which represents a max of differences between chars in both arrays and return true or false depending on if arrays are similar differing at most with d chars</p> <p>so for example</p> <p>Having </p> <pre><code>char[] a1 = { 's', 't', 'a', 'f', 'f' }; char[] a2 = { 's', 't', 'a', 'c', 'k' }; </code></pre> <p>and having d=2 would return true because those arrays are almost similar</p> <p>I was thinking on making a for loop and comparing each char and having a counter of maxDiference and if the counter exceeds return false like:</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace auxros { class Program { static void Main(string[] args) { char[] a1 = { 's', 't', 'a', 'f', 'f' }; char[] a2 = { 's', 't', 'a', 'c', 'k' }; bool areDifferent = CharMismatches(a1, a2, 1); System.Console.WriteLine("The arrays are diferent at most by one char? " + areDifferent); areDifferent = CharMismatches(a1, a2, 2); System.Console.WriteLine("The arrays are diferent at most by two chars? " + areDifferent); areDifferent = CharMismatches(a1, a2, 3); System.Console.WriteLine("The arrays are diferent at most by three chars? " + areDifferent); } static bool CharMismatches(char[] a1, char[] a2, int d) { int mismatches = 0; for (int i = 0; i &lt; a1.Length; i++) { if (!a1[i].Equals(a2[i])) { mismatches++; } if (mismatches == d) { return true; } } if (mismatches &lt;= d) { return false; } return true; } } } </code></pre> <p>Is there a more efficient way to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload