Java left rotation string introduction _java

Source: Internet
Author: User
Tags strlen

Topic: Defines the left rotation of a string: moves several characters in front of the string to the end of the string. If the string abcdef left rotate 2 bits to get the string cdefab. Implement the function of the left rotation of the string. Requires time for a string operation of length n (n), and secondary memory is O (1).

Analysis: If you do not take into account the limitations of time and space complexity, the easiest way to do this is to think of the problem as dividing the string into two parts, exchanging the two parts in a rotating operation. So we can create a new auxiliary space with a length of n+1, copy the second half of the original string to the first half of the new space, and copy the first half of the original string to the second half of the new space. It is not difficult to see that the time complexity of this idea is O (n), the required auxiliary space is also O (n).

The next line of thought might be a little bit of a hassle. Let's assume that the string is rotated to the left m bit. So we first save the No. 0 character, put the M characters to the No. 0 position, in the first 2m characters to the position of the first m ... And so on, move to the last one to move the character, and then put the original No. 0 character in the position you just moved. Then save the 1th character and move the first M+1 element to the 1th position ... Repeat the previous step of processing the No. 0 character until you finish processing the previous m characters.

This idea is relatively easy to understand, but when the length of the string n is not the integer times m, the writing program will have some trouble, interested friends can try it on their own. Since there are better ways to do this, I don't have the code for this idea.

We still think of the string as a two-segment, bit XY. The left rotation is equivalent to turning the string xy into YX. We first define a flip operation on the string, which is to flip the sequence of characters in the string. Flip the x over to the XT. There is obviously a (XT) t=x.

We first flip the x and y segments separately, so we can get xtyt. The XTYT is then flipped to get the (XTYT) t= (YT) T (XT) T=yx. That's exactly what we're looking for.

Analysis here we go back to the original topic. All we have to do is divide the string into two paragraphs, the first of which is the previous m, and the remaining characters to the second paragraph. To define a function that flips the string, flip it three times on the previous steps. Time complexity and space complexity all meet the requirements.

Copy Code code as follows:

public class Test_21 {
public static void Main (string[] args) {
StringBuilder str = new StringBuilder ("ABCDE");
int index = 5;
System.out.println (Leftturn (Str,index));
}
public static String Leftturn (StringBuilder sb,int index) {
int strlen = Sb.length ();
if (SB!=null&&index>=0&&index<=strlen) {
int firststart = 0;
int firstend = index-1;
int secondfirst = index;
int secondend = strlen-1;




ReverseString (Sb,firststart,firstend);
ReverseString (Sb,secondfirst, secondend);
ReverseString (Sb,firststart,secondend);


return sb.tostring ();
}
return null;

}
public static void ReverseString (StringBuilder str,int begin, int end) {

while (Begin<=end) {
Char temp = Str.charat (begin);
Str.setcharat (Begin, Str.charat (end));
Str.setcharat (end, temp);
begin++;
end--;
}
System.out.println (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.