Created
May 12, 2021 06:14
-
-
Save NimaGhaedsharafi/13ad13ae5a0f6d61e95e767de6811569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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