Open In App

Java Program for Minimum rotations required to get the same string

Last Updated : 26 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
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 : 1

The idea is based on below post. A Program to check if strings are rotations of each other or not 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. 

Java




// Java program to determine minimum number
// of rotations required to yield same
// string.
 
import java.util.*;
 
class GFG
{
    // Returns count of rotations to get the
    // same string back.
    static int findRotations(String str)
    {
        // tmp is the concatenated string.
        String tmp = str + str;
        int n = str.length();
     
        for (int i = 1; i <= n; i++)
        {
     
            // substring from i index of original
            // string size.
             
            String substring = tmp.substring(
                      i, i+str.length());
     
            // if substring matches with original string
            // then we will come out of the loop.
            if (str.equals(substring))
                return i;
        }
        return n;
    }
 
    // Driver Method
    public static void main(String[] args)
    {
            String str = "aaaa";
        System.out.println(findRotations(str));
    }
}
/* This code is contributed by Mr. Somesh Awasthi */


Output: 

3

Time Complexity: O(n2) Please refer complete article on Minimum rotations required to get the same string for more details!
Auxiliary Space: O(n), The extra space is used to store the copied string in tmp variable.



Similar Reads

C++ Program for Minimum rotations required to get the same string
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 : 1 The idea is based on below post.A Program to check if strings are rotations of each other or not Step 1: Initialize result = 0 (Here result is count of rotationsStep 2: Take a tempor
2 min read
Php Program for Minimum rotations required to get the same string
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 : 1 The idea is based on below post. A Program to check if strings are rotations of each other or not Step 1 : Initialize result = 0 (Here result is count of rotations) Step 2 : Take a t
2 min read
Python3 Program for Minimum rotations required to get the same string
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 : 1 Method 1: The idea is based on below post.A Program to check if strings are rotations of each other or notStep 1 : Initialize result = 0 (Here result is count of rotations)Step 2 : Ta
2 min read
Minimum rotations required to get the same String | Set-2
Given a string, we need to find the minimum number of rotations required to get the same string. In this case, we will only consider Left rotations. Examples: Input : s = "geeks" Output : 5 Input : s = "aaaa" Output :1 Naive approach: The basic approach is to keep rotating the string from the first position and count the number of rotations until w
10 min read
Minimum rotations required to get the same string
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 : 1 The idea is based on below post.A Program to check if strings are rotations of each other or not Step 1 : Initialize result = 0 (Here result is count of rotations) Step 2 : Take a te
11 min read
Count clockwise array rotations required to maximize count of array elements present at indices same as their value
Given an array arr[] consisting of a permutation of first N natural numbers, the task is to find the minimum number of clockwise circular rotations of the array required to maximise the number of elements satisfying the condition arr[i] = i ( 1-based indexing ) where 1 ? i ? N. Examples: Input: arr[] = {4, 5, 1, 2, 3}Output: 3Explanation: Rotating
7 min read
Java Program to Count of rotations required to generate a sorted array
Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: Sorted array {1, 2, 2, 2, 2} after 1 anti-clockwise
4 min read
Java Program to Count rotations required to sort given array in non-increasing order
Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Explanation: Two anti-clockwise rotations are required
3 min read
Java Program to Maximize count of corresponding same elements in given permutations using cyclic rotations
Given two permutations P1 and P2 of numbers from 1 to N, the task is to find the maximum count of corresponding same elements in the given permutations by performing a cyclic left or right shift on P1. Examples:  Input: P1 = [5 4 3 2 1], P2 = [1 2 3 4 5] Output: 1 Explanation: We have a matching pair at index 2 for element 3.Input: P1 = [1 3 5 2 4
4 min read
Java Program to Find Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
Given a numeric string target of length N and a set of numeric strings blocked, each of length N, the task is to find the minimum number of circular rotations required to convert an initial string consisting of only 0's to target by avoiding any of the strings present in blocked at any step. If not possible, print -1.Note: A single rotation involve
4 min read
Article Tags :
Practice Tags :