How to check if second string a rotated version of first string in PHP

1 Answer

0 votes
function isSecondStringRotatedOfFirstString($str1, $str2) {
    if (strlen($str1) != strlen($str2)) {
        return false;
    }
    
    $concatenated = $str1 . $str1;
    
    return strpos($concatenated, $str2) != -1;
}
        
$first = "abcdefg";
$second = "cdefgab";

echo isSecondStringRotatedOfFirstString($first, $second) ? "yes" : "no";



 
/*
run:
 
yes
  
*/

 



answered Mar 25, 2024 by avibootz

Related questions

...