Open In App

PHP Program to check a string is a rotation of another string

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given the two strings we have to check if one string is a rotation of another string. Examples:

Input : $string1 = "WayToCrack",
        $string2 = "CrackWayTo";
Output : Yes

Input : $string1 = "WillPower" 
        $string2 = "lliW";
Output : No.

The above problem can be easily solved in other languages by concatenating the two strings and then would check if the resultant concatenated string contains the string or not. But in PHP we will use an inbuilt function to solve the problem. The inbuilt functions used are:

1. strpos():

strpos() function generally accepts the two parameters first one to specify the string to be searched and the other one to find in the specified string.

In PHP solution strpos() will give the last position of string if found in the specified string. Below is the implementation of above approach.

<?php
    function rotation_string($string1, $string2)
    {
        // if both strings are not same length then stop
        if (strlen($string1) != strlen($string2))
           echo "No";
         
         // concatenate $string1 to itself, if both
         // strings are of same length
         if (strlen($string1) == strlen($string2))
            $string1 = $string1.$string1;
          
         if (strpos($string1, $string2) > 0)
            echo "Yes";
         else 
            echo "No";
    }

    // Driver code
    $string1 = "WayToCrack";
    $string2 = "CrackWayTo";        
    rotation_string($string1, $string2);
?>

Output
Yes

Using Substring Comparison

You can determine if one string is a rotation of another string by checking if one string is a substring of the other string concatenated twice.

Example:

<?php
function isRotation($string1, $string2) {
    // Check if both strings are of the same length and not empty
    if (strlen($string1) != strlen($string2)) {
        return "No";
    }
    
    // Concatenate string1 with itself
    $concatenated = $string1 . $string1;
    
    // Check if string2 is a substring of the concatenated string
    if (strpos($concatenated, $string2) !== false) {
        return "Yes";
    } else {
        return "No";
    }
}

// Example usage
$string1 = "WayToCrack";
$string2 = "CrackWayTo";
$result = isRotation($string1, $string2);
echo $result; // Output: Yes

?>

Output
Yes

Using str_contains() Function

The str_contains() function checks if a given substring is present within another string. By concatenating the first string with itself, you can check if the second string is a substring of the resulting string.

Example:

<?php
function isRotation($string1, $string2) {
    // Check if lengths are different
    if (strlen($string1) !== strlen($string2)) {
        return "No";
    }

    // Concatenate $string1 with itself
    $concatenated = $string1 . $string1;

    // Check if $string2 is a substring of the concatenated string
    if (str_contains($concatenated, $string2)) {
        return "Yes";
    } else {
        return "No";
    }
}

// Examples
$string1 = "WayToCrack";
$string2 = "CrackWayTo";
echo "Is '$string2' a rotation of '$string1'? " . 
  isRotation($string1, $string2) . PHP_EOL;

$string1 = "WillPower";
$string2 = "lliW";
echo "Is '$string2' a rotation of '$string1'? " . 
  isRotation($string1, $string2) . PHP_EOL;
?>

Output:

Is 'CrackWayTo' a rotation of 'WayToCrack'? Yes
Is 'lliW' a rotation of 'WillPower'? No


Next Article

Similar Reads

three90RightbarBannerImg