Introduction to Java's left rotation string

  • 2020-04-01 01:30:43
  • OfStack

Title: to define the left rotation of a string: to move the first few characters of the string to the end of the string. Such as the string abcdef left rotation 2 bits to get the string cdefab. Implement a function that rotates the string to the left. The time required for a string operation of length n is O(n) and the auxiliary memory is O(1).

Analysis: without considering the limitations of time and space complexity, it is easiest to think of this problem as dividing a string into two parts and swapping them by rotation. So we can create a new auxiliary space of length n+1, and copy the second half of the original string to the first half of the new space, and then 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), and the auxiliary space required is also O(n).

The next line of thinking might be a little trickier. Let's say we rotate the string m bits to the left. So let's save the 0th character, put the MTH character in the 0th position, put the 2m character in the MTH position... And so on, all the way to the last character that you can move, and then you put the 0th character where you moved it. Then save the first character, move the m+1 element to the first position... Repeat the previous steps for the 0th character until the previous m characters are processed.

This idea is still relatively easy to understand, but when the length of the string n is not an integer multiple of m, writing the program will be a bit troublesome, interested friends can try it on their own. I won't provide the code for this line of thinking, since there are better methods to follow.

Again, we can think of a string as having two segments, the XY bits. The left rotation is the same thing as changing the string XY to YX. We first define a flip operation on the string, which is to flip the sequence of characters in the string. I'm going to flip X and call it XT. Well, of course we have XT, T is equal to X.

We're going to flip the X and Y segments, and we're going to get XTYT. And then I flip XTYT, and I get (XTYT)T=(YT)T(XT)T=YX. Exactly what we expected.

So let's go back to our original problem. All we need to do is split the string into two segments, the first with m characters and the rest into the second. Define a function that flips the string, and do the same thing three times. Time complexity and space complexity meet the requirements.


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);
 }
}


Related articles: