Given this input s1 = "dadxx"
s2 = "ddxx"
I'd expect the output to contain a bunch of a,b
pairs wherever each character in s1
matched a character in s2
and vice versa (duplicates allowed). Among those pairs would be 0,0
because s1[0]
and s2[0]
are both equal to d
.
The problem is that my output doesn't contain 2,1
even though s1[2]
and s2[1]
are both equal to d
.
Can someone fix my algorithm or make a better one?
Here's a JSFiddle if it helps.
Here's my code:
// For each char, see if other string contains it
s1 = 'dadxx'
s2 = 'ddxx'
matchChars(s1,s2)
matchChars(s2,s1)
function matchChars(a,b) {
for (i = 0; i < a.length; i++) {
found = b.indexOf(a[i])
if (found >= 0) {
if (a===s1) console.log(i,found)
else console.log(found,i)
}
}
}