P
Peter Duniho
kndg said:Your comments was very helpful.
Here is the bug-fix version.
Any inputs is highly appreciated.
Looks lots better. You do seem to have addressed both of the issues I
mentioned.
That said, note that you have an implicit third inner loop now. Your
code is equivalent to this (mostly…I added a couple of things):
for (int i = 0; i < str1.Length; i++)
{
int cchLeft1 = str1.Length - i;
// Can't do better than what we've got…bail!
if (cchLeft1 <= matchLength)
{
break;
}
for (int j = 0; j < str2.Length; j++)
{
int cchLeft2 = str2.Length - j;
if (cchLeft2 <= matchLength)
{
break;
}
int cchMax = Math.Min(cchLeft1, cchLeft2);
int ich1 = i;
for (int ich2 = j; ich2 < j + cchMax; ich1++, ich2++)
{
if (str2[ich2] != str1[ich1])
{
break;
}
}
if (ich1 - i > matchLength)
{
matchIndex = j;
matchLength = ich1 - i;
}
}
}
(Actually, not quite equivalent, because in rewriting your code, I found
another bug: you only updated matchIndex and matchLength if you found a
non-equal character; if it happened that the intersection was the entire
second string, you'd never hit the code to set matchIndex and
matchLength. I moved the check out of the inner-most loop – the new
third loop – so it's always evaluated, even if you never find a
non-equal character).
(I also added a minor optimization so that the code doesn't waste time
looking for more matches if it's already found one at least as large as
is possible to find in the remaining characters).
I think that writing the third loop explicitly, and in fact changing the
second loop from a while to a for so that it matches the other two,
helps the code flow a bit better. YMMV.
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
I'm pleased to see that there was in fact a three-loop solution (at
least, I think there is…I haven't been able to find anything fatally
wrong with this latest solution), but I was really hoping I had been
wrong and that a two-loop one was going to come out of this.
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
Of course, the fact remains that the original problem statement is
poorly defined. As we can see from the code you posted most recently
and my modifications, some string pairs may well have two or more
"intersections" of the same length, but of course we can return only one
(cue "Highlander" music…
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
to me, at least approaching the problem this way (there are other
definitions of "intersection" that may make more sense).
Pete