PHP Program to check a string is a rotation of another string (original) (raw)
Last Updated : 19 Jul, 2024
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 `
string1=string1 = string1=string1.$string1; if (strpos($string1, $string2) > 0) echo "Yes"; else echo "No"; } // Driver code $string1 = "WayToCrack"; $string2 = "CrackWayTo"; rotation_string($string1, $string2); ?>`
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 `
concatenated=concatenated = 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";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>i</mi><mi>s</mi><mi>R</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">result=isRotation(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal"style="margin−right:0.00773em;">R</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>string1,string2 = "CrackWayTo"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>i</mi><mi>s</mi><mi>R</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">result = isRotation(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mord mathnormal">o</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>string1, string2="CrackWayTo";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>i</mi><mi>s</mi><mi>R</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">result=isRotation(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal"style="margin−right:0.00773em;">R</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>string1,string2); echo $result; // 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 `
concatenated=concatenated = 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