Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first thing to answer is </p> <pre><code>What is a mismatch ? </code></pre> <p>Mismatch is when the characters are out of order. </p> <pre><code>When are characters out of order ? </code></pre> <p>First assumption : The 2 strings are inter-convertible.</p> <p>Second assumption : Both strings are of same length.</p> <p>So an out of order means that for a character <strong>i</strong> in first string, there <strong>certainly</strong> exists another character at position <strong>j</strong> in the second string. </p> <pre><code>When are swaps required ? </code></pre> <p>So for any given index <strong>i</strong>, if the characters in both the strings are not equal. There is a mismatch. How many mismatches ? 2 mismatches. Since the mismatched character from first string will cause a mismatch in the second string at another position <code>j</code>. So the total number of mismatches will always be a multiple of 2. </p> <p>One swap corrects position of 2 characters. So the optimal number of mismatches is half the total number of mismatches.</p> <p>Here is some sample code.</p> <pre><code>public class CharacterMismatch { /** * Returns -1 if not possible * @param first * @param second * @return */ public static int maxSwaps(char first[], char second[]) { if (first.length != second.length) { return -1; } int freqFirst[] = new int[Character.MAX_VALUE], freqSecond[] = new int[Character.MAX_VALUE]; int len = first.length; for (int i = 0; i &lt; len; ++i) { freqFirst[first[i]]++; freqSecond[second[i]]++; } for (int i = 0; i &lt; len; ++i) { if (freqFirst[i] != freqSecond[i]) { return -1; } } //count mismatches. That is the minimal number of swaps required. int swaps = 0; for (int i = 0; i &lt; len; ++i) { if (first[i] != second[i]) { swaps++; } } return swaps / 2; } public static void main(String[] args) { String first[] = { "aabbccdd", "abcc", "abccab" }; String second[] = { "ddbbccaa", "accb", "abcabc"}; for (int i = 0; i &lt; first.length; ++i) { System.out.printf("%s %s =&gt; %d%n", first[i], second[i], maxSwaps(first[i].toCharArray(), second[i].toCharArray())); } } } </code></pre> <p>Returns</p> <pre><code>aabbccdd ddbbccaa =&gt; 2 abcc accb =&gt; 1 abccab abcabc =&gt; 1 </code></pre> <p>Complexity is O(n).</p> <p>There is some boiler plate code which checks whether they are really inter-convertible by checking the frequency of each character in both strings and returns -1 if not possible.</p> <p><strong>Edit:</strong></p> <p>Sorry this works only when the max number of swaps for every mismatched character is at most one. </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.
    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