I need to find the no. of similar characters between two strings using JS/PHP
Example str1: Mack str2: Michelle
Similar Characters: "M" "C" similar character count: 2
I need to find the no. of similar characters between two strings using JS/PHP
Example str1: Mack str2: Michelle
Similar Characters: "M" "C" similar character count: 2
After your precision, here is my solution:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0, find = -1;
for(var i = 0; i < str1.length; i++)
{
find = str2.indexOf(str1.charAt(i));
if(find > -1)
{
counter++;
str2 = str2.substr(0, find) + str2.substr(find + 1);
}
}
alert("Result: "+counter);
It works with the 2 examples you gave us:
Michellle and Michelle = 8 Sneha and naveen = 3