Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String matching algorithm

Say I have 3 strings. And then 1 more string.
Is there an algorithm that would allow me to find which one of the first 3 strings matches the 4th string the most?
None of the strings are going to be exact matches, I'm just trying to find the closest match.
And if the algorithm already exists in STL, that would be nice.

Thanks in advance.

like image 958
Josh Avatar asked Nov 28 '25 17:11

Josh


2 Answers

You don't specify what exactly you mean by "matches the most", so I assume you don't have precise requirements. In that case, Levenshtein distance in a reasonable metric. Simply compute the Levenshtein distance between each of the three strings and the fourth, and pick the one that gives the lowest distance.

like image 114
NPE Avatar answered Nov 30 '25 07:11

NPE


You can implement the Levenshtein Distance algorithm, it provides a very nice measure of how close a match between two strings you have. It measures how many keystrokes you need to make in order to turn one string into the other. You can find a C++ implementation here.

Compute Levenshtein Distance between string #4 and the three strings that you have. Pick the string with the smallest distance.

like image 22
Sergey Kalinichenko Avatar answered Nov 30 '25 05:11

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!