PHP Program for Minimum rotations required to get the same string Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string, we need to find the minimum number of rotations required to get the same string. Examples:Input : s = "geeks" Output : 5 Input : s = "aaaa" Output : 1Algorithm: Step 1: Initialize result = 0 (Here result is count of rotations) Step 2: Take a temporary string equals to original string concatenated with itself. Step 3: Now take the substring of temporary string of size same as original string starting from second character (or index 1). Step 4: Increase the count.Step 5: Check whether the substring becomes equal to original string. If yes, then break the loop. Else go to step 2 and repeat it from the next index. Below is the implementation of the above algorithm: PHP <?php // PHP program to determine minimum // number of rotations required to // yield same string. // Returns count of rotations // to get the same string back. function findRotations($str) { // tmp is the concatenated string. $tmp = $str . $str; $n = strlen($str); for ($i = 1; $i <= $n; $i++) { // substring from i index // of original string size. $substring = substr($tmp, $i, $n); // if substring matches with // original string then we will // come out of the loop. if ($str == $substring) return $i; } return $n; } // Driver code $str = "abc"; echo findRotations($str), "\n"; // This code is contributed // by Sachin ?> Output3 Complexity Analysis:Time Complexity: O(n2) Auxiliary Space: O(n), The extra space is used to store the copied string in tmp variable.Please refer complete article on Minimum rotations required to get the same string for more details! Comment More infoAdvertise with us Next Article PHP Program for Minimum rotations required to get the same string K kartik Follow Improve Article Tags : Strings Web Technologies PHP PHP Programs DSA rotation +2 More Practice Tags : Strings Similar Reads PHP Program to Rotate the matrix right by K times Given a matrix of size N*M and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4A simple yet effective approach is to consider e 2 min read PHP Program to check if strings are rotations of each other or not Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm:areRotations(str1, str2): 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = 2 min read PHP Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 2 min read PHP Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1.Examples: Input: s1 = "for", s2 = "geeksforgeeks"Output: 5Explanation: String "for" is present as a substring of s2.Input: s1 = "practice", s2 = "geeksforgeeks"Output: -1.Ex 2 min read PHP Program to Count Rotations Divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4, 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations ar 2 min read Like