PublicclassHealper {*** @ paramstr original string * @ return string cycle * publicstaticintgetCycle (Stringstr) {obtain the character charendCharstr. charAt (str. length ()-1); get the first position where the last character appears in the original string intindexstr. indexOf (endC
Public class Healper {/*** @ param str original String * @ return String cycle */public static int getCycle (String str) {// get the last character char endChar = str. charAt (str. length ()-1); // obtain the first position where the last character appears in the original string. int index = str. indexOf (endC
Public class Healper {
/**
* @ Param str original string
* @ Return string cycle
*/
Public static int getCycle (String str)
{
// Obtain the end character
Char endChar = str. charAt (str. length ()-1 );
// Obtain the first position of the last character in the original string.
Int index = str. indexOf (endChar );
// Truncate a substring that may be a periodic string
String subString = str. substring (0, index + 1 );
Return getStringCycle (subString, str );
}
/**
*
* @ Param subString
* @ Param str original string
* @ Return string cycle
*/
Private static int getStringCycle (String subString, String str)
{
// Define the variable to save the initial length of the original string
Int len = str. length ();
// Define the period in which the variable saves the string
Int num = 0;
// If the substring length is not proportional to the original string length, it cannot be a periodic string
If (str. length () % subString. length ()! = 0)
Return 1;
While (str! = "")
{
String temp = str;
// Replace the first substring with an empty string and assign a new value to the original string.
Str = str. replaceFirst (subString ,"");
// If the length of the character string is equal to the length of the temporary character string, this character is not replaced.
If (str. length () = temp. length ()){
Break;
} Else
{
Num ++;
}
}
// If the string length divided by the obtained period equals to the length of the sub-string, this cycle is returned; otherwise, 1 is returned.
Return len/num = subString. length ()? Num: 1;
}
}
Import java. util. collections;
Public class mainclass {
Public static void main (String [] args ){
S = new partition (System. in );
System. out. print ("enter any string :");
String str = s. next ();
System. out. println ("the string's cycle is:" + Healper. getCycle (str ));
}
}