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