How to check if a string is rotation of another string in PHP

1 Answer

0 votes
function isRotation($s1, $s2) { 
        return (strlen($s1) == strlen($s2) && (strpos($s1.$s1, $s2) !== false)); 
} 
  

$s1 = "abbc"; 
$s2 = "cabb"; 

echo isRotation($s1, $s2) ? "yes" : "no";

echo "<br />";
        
$s1 = "abbc"; 
$s2 = "bbac"; 
  
echo isRotation($s1, $s2) ? "yes" : "no";

    
 
      
      
/*
run:
      
yes
no
       
*/

 



answered Sep 26, 2019 by avibootz

Related questions

1 answer 111 views
1 answer 105 views
1 answer 178 views
1 answer 132 views
1 answer 180 views
1 answer 146 views
...