Skip to content

Instantly share code, notes, and snippets.

@NimaGhaedsharafi
Created May 12, 2021 06:14
Show Gist options
  • Save NimaGhaedsharafi/13ad13ae5a0f6d61e95e767de6811569 to your computer and use it in GitHub Desktop.
Save NimaGhaedsharafi/13ad13ae5a0f6d61e95e767de6811569 to your computer and use it in GitHub Desktop.
<?php
$str0 = "at";
$str1 = "cat";
function oneEditApart($str0, $str1)
{
if (abs(strlen($str0) - strlen($str1)) > 1) {
return false;
}
// if have the same length!
if (strlen($str0) - strlen($str1) == 0) {
$dif = 0;
for ($i = 0; $i < strlen($str0); $i++) {
if ($str0[$i] != $str1[$i]) {
$dif++;
}
if ($dif > 1) {
return false;
}
}
return true;
}
// else
if (strlen($str0) < strlen($str1)) {
$tmp = $str1;
$str1 = $str0;
$str0 = $tmp;
}
return str_contains($str0, $str1);
}
var_dump(OneEditApart($str0, $str1));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment