Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Haven't tested this, but here's my thought:</p> <ol> <li>Quicksort both strings in place, so you have an ordered sequence of characters</li> <li>Keeping an index into both strings, compare the "next" character from each string, pick and output the first one, incrementing the index for that string.</li> <li>Continue until you get to the end of one of the strings, then just pull unique values from the rest of the remaining string.</li> </ol> <p>Won't use additional memory, only needs the two original strings, two integers, and an output string (or StringBuilder). As an added bonus, the output values will be sorted too!</p> <p>Part 2: This is what I'd write (sorry about the comments, new to stackoverflow):</p> <pre><code>private static string intersect(string left, string right) { StringBuilder theResult = new StringBuilder(); string sortedLeft = Program.sort(left); string sortedRight = Program.sort(right); int leftIndex = 0; int rightIndex = 0; // Work though the string with the "first last character". if (sortedLeft[sortedLeft.Length - 1] &gt; sortedRight[sortedRight.Length - 1]) { string temp = sortedLeft; sortedLeft = sortedRight; sortedRight = temp; } char lastChar = default(char); while (leftIndex &lt; sortedLeft.Length) { char nextChar = (sortedLeft[leftIndex] &lt;= sortedRight[rightIndex]) ? sortedLeft[leftIndex++] : sortedRight[rightIndex++]; if (lastChar == nextChar) continue; theResult.Append(nextChar); lastChar = nextChar; } // Add the remaining characters from the "right" string while (rightIndex &lt; sortedRight.Length) { char nextChar = sortedRight[rightIndex++]; if (lastChar == nextChar) continue; theResult.Append(nextChar); lastChar = nextChar; } theResult.Append(sortedRight, rightIndex, sortedRight.Length - rightIndex); return (theResult.ToString()); } </code></pre> <p>I hope that makes more sense.</p>
 

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