Introduction to left-rotated strings in Java

Source: Internet
Author: User

Title: Define the left rotation operation of a string: Move several characters before the string to the end of the string. For example, the string abcdef is rotated two places to the left to obtain the string cdefab. Please implement the left rotation function of the string. Requires that the complexity of the string operation with a length of N is O (n), and the auxiliary memory is O (1 ).

Analysis: if you do not consider the restrictions of Time and Space complexity, the simplest method is to divide the string into two parts, and switch the two parts through the rotation operation. So we can create a new auxiliary space with a length of N + 1 and copy the half of the original string to the first half of the new space, copy the first half of the original string to the second half of the new space. It is not hard to see that the time complexity of this idea is O (n), and the auxiliary space required is O (n ).

The next idea may be a little more troublesome. Let's assume that the string is rotated to the left M bit. So we saved the 0th characters, put the M character at the 0th position, and put the 2 M character at the M character... And so on, always move to the last character that can be moved, and finally put the original 0th characters at the position just moved. Then save the 1st characters and move the m + 1 element to the 1st position... Repeat the step to process the first 0th characters until the previous m characters are processed.

This idea is easy to understand, but when the length of the string N is not an integer multiple of M, writeProgramIt may be a little troublesome. If you are interested, you can try it on your own. As we need to introduce better methods belowCodeI will not provide it.

We still regard the string as a string consisting of two segments, with the XY value. The left rotation is equivalent to converting the string XY into Yx. First, we define a flip operation on the string, that is, the character order in the flip string. After turning X, record it as XT. Obviously (XT) t = x.

We first perform the flip operation on the X and Y segments respectively to obtain the xtyt. Then, perform the xtyt flip operation to obtain (xtyt) t = (yt) T (XT) t = Yx. This is exactly the expected result.

After analysis, we can return to the original question. All we need to do is to divide the string into two segments. The first segment is the first M character, and the remaining characters are divided into the second segment. Define another function to flip the string and follow the previous steps to flip it three times. Both time complexity and space complexity are required.

Copy code Code: 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.