Faced such a problem: it is necessary to implement an algorithm for comparing two lines - the difference should not exceed 2 characters (case change is not considered).
What did you do:
I translate all the letters in lower case. I break lines into an array.
If the difference in the length of the array> 2 - error
If the number of changed characters is> 4 (delete 2, add 3; replace 2 characters, add one, etc.) - error
The code is implemented on the example of PHP:
$oldValue = preg_split('//u', (mb_strtolower($oldValue)),-1, PREG_SPLIT_NO_EMPTY); $newValue = preg_split('//u', (mb_strtolower($newValue))), -1, PREG_SPLIT_NO_EMPTY); if(abs((count($oldValue) - count($newValue))) > 2 || count(array_diff($newValue, $oldValue)) + count(array_diff($oldValue ,$newValue)) > 4) { throw new Exception('bla-bla'); } Successfully work out on pairs:
Ивановвввв -> Иванов (Exception) Иванов -> Иванв Иванов -> Иван иИванов -> Иванов ииванова -> Иванов Иванокк -> Иванов Пелоге -> Пелагея But it does not work correctly on pairs:
Пелашш -> Пелагея Someone already faced with this kind of tasks?
How to find the optimal solution without listing all possible cases?