Calculate the string cycle. For example, the abcabc cycle is 2.

Source: Internet
Author: User
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 ));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.