java Implementation of Left Rotation String

  • 2021-07-01 07:25:34
  • OfStack

There is a shift instruction in assembly language called Loop Left Shift (ROL). Now there is a simple task, which is to simulate the operation result of this instruction with a string. For a given sequence of characters S, please output the sequence after cyclically shifting the K bit to the left. For example, the character sequence S= "abcXYZdef" requires the output of the output after the loop is shifted left by 3 bits, that is, "XYZdefabc". Isn't it very simple? OK, fix it!

Code

Solution 1

The most intuitive way is to move the characters that need to be shifted in turn to the last, but each character needs to move the length of the array by-1. If the length of the array is n and you need to move k bits, you need to move k * (n-1) in total


public static String leftRotateString(String str, int n) {
    if (Strings.isNullOrEmpty(str)) {
      return str;
    }
    if (n < 0 || n >= str.length()) {
      return str;
    }
    char[] strArray = str.toCharArray();
    while (n-- > 0){
      //  In the direct exchange mode, the part that needs to be shifted is exchanged n-1 Second move to the left 
      //  For example abcde To move 2 Bit, that is cdead
      // 1.  Will a Move to the end, where it is bcdea
      // 2.  Will b Move to the end, where it is cdeab
      for (int i = 0; i < strArray.length - 1; i++) {
        swap(strArray, i, i + 1);
      }
    }
    return new String(strArray);
  }
 
  private static void swap(char[] str, int i, int j) {
    char temp = str[i];
    str[i] = str[j];
    str[j] = temp;
  }


Solution 2

With the help of string inversion, for example, "ab" corresponds to "ba" and "xyz" corresponds to "zyx", it takes a total of 3 steps to get the expected thought

Reverse the part to be shifted, and the "abcXYZdef" operation is followed by "cbaXYZdef" Reverse the rest, and the "cbaXYZdef" operation is followed by "cbafedZYX" Inverts the whole string, followed by "XYZdefabc" after the "cbafedZYX" operation

public static String leftRotateString2(String str, int n) {
    if (Strings.isNullOrEmpty(str)) {
      return str;
    }
    if (n < 0 || n >= str.length()) {
      return str;
    }
    char[] strArray = str.toCharArray();
    //  With the help of inversion, points 3 Step 
    // 1.  Reverse the part to be shifted 
    // 2.  Reverse the rest 
    // 3.  Overall reversal 
    reverse(strArray, 0, n - 1);
    reverse(strArray, n, strArray.length - 1);
    reverse(strArray, 0, strArray.length - 1);
    return new String(strArray);
  }
 
  /**
   *  Invert the string, and the two ends are exchanged in turn to complete the inversion 
   * @param str
   * @param start
   * @param end
   */
  private static void reverse(char[] str, int start, int end) {
    while (start < end) {
      swap(str, start, end);
      start++;
      end--;
    }
  }
 
  private static void swap(char[] str, int i, int j) {
    char temp = str[i];
    str[i] = str[j];
    str[j] = temp;
  }


Related articles: