Rotating string of daily question (super detailed, super hard core)

Posted by cityguru on Wed, 02 Feb 2022 15:21:10 +0100

Introduction to the content of the title (left-handed transport operator)

Implement a function that can k characters in a left-handed string.

ABCD left turns one character to get BCDA

ABCD left rotation two characters to get CDAB

Idea 1: temporary array method 1

  1. Array arr the contents of the array should be moved forward by K bits, and the first k moves to the end of the ARR array.

  2. First, create a temporary array temp with the same length as the original array arr.

  3. Assign the first k elements in the arr array to the last k bits of the temp array.

  4. Then shift the first k of the arr array to the first k of the temp array. In this way, the rotated element order is presented in the temp array.

  5. Then assign the overall content of the temp array to the original array arr.

    Related diagrams

    code implementation

#include <stdio.h>
#include <string.h>
#include <assert.h>
void rotate(char* arr, int len, int k)
{
	assert(arr != NULL);  //Array length cannot be empty
	assert(k <= len);     //The passed k must be less than the length of the array
	char temp[5] = { 0 };
	int i = 0;
    //Move the first k bits of the arr array to the last k bits of the temporary array
	for (i = 0; i < k; i++)
	{
		temp[len - k + i] = arr[i];
	}
	//Move the last k bits of the arr array to the first k bits of the temporary array
	for (i = k; i < len; i++)
	{
		temp[i - k] = arr[i];
	}
	//Move the contents of temp array to arr array
	for (i = 0; i < len; i++)
	{
		arr[i] = temp[i];
	}
}
int main()
{
	//First assign the first k characters in arr to the back of temp, and then move the last few bits of K in arr to the first few bits of temp
	char arr[] = "ABCD";
	int len = strlen(arr);
	int k = 0;
	scanf("%d", &k);  //Number of digits moved forward
	rotate(arr, len, k);
	printf("%s", arr);
	return 0;
}

Idea 2: temporary array method 2

  1. Create a temporary array

  2. Transfer the first k bits (including k bits) of the arr array to the array first.

  3. The position of the first k bits (including k bits) of the arr array has been vacant at this time. Move the last k bits in the arr array forward by K bits

  4. Then attach the contents of the temporary array to the back of the arr array

    Relevant diagrams:

Code implementation:

#include <stdio.h>
#include <string.h>
#include <assert.h>
void rotate(char* arr, int len, int k)
{
	assert(arr != NULL);  //The array passed in cannot be empty
	assert(k <= len);   //The number of rotations passed in must be less than or equal to the character length
	char temp[5] = { 0 };  //Create a temporary array, 
	int i = 0;
	//Use a temporary array to store the elements in the first k arr arrays
	for (i = 0; i < len; i++)
	{
		temp[i] = arr[i];    
	} 
	//Then move the elements in the array left by arr forward by k bits
	for (i = k; i < len; i++)
	{
		arr[i - k] = arr[i];
	}
	//Insert the numbers in the temporary array into the arr array
	for (i = len - k ; i < len; i++)
	{
		arr[i] = temp[i - (len - k)];
	}
}
int main()
{
	char arr[] = "ABCD";
	int len = strlen(arr); //Calculate string length
	int k = 0;
	scanf("%d", &k); //Turn k characters to do first
	rotate(arr, len,k);
	printf("%s", arr);
	return 0;
}

Idea 3: temporary variable circulation method

  • The above two algorithms have the same defect, that is to establish a temporary array and limit the length of the temporary array, which seriously affects the flexibility of the algorithm and improves the spatial complexity,
  • The following is the third algorithm, which reduces the spatial complexity
  1. Extract the first k elements from the arr array one at a time
  2. In the process of extraction, move the last k elements forward
  3. Add the extracted elements to the arr array in turn

Relevant diagrams:

Code implementation:

#include <stdio.h>
#include <string.h>
#include <assert.h>
void rotate(char* arr, int len, int k)
{
	assert(arr != NULL);
	assert(k <= len);
	int i = 0;
	for (i = 0; i < k; i++)
	{
		char temp = arr[0];
		int j = 0;
		for (j = 0; j < len-1; j++)
		{
			arr[j] = arr[j + 1];
		}
		arr[len - 1] = temp;
	}
}
int main()
{
	char arr[] = "ABCDE";
	int k = 0;
	scanf("%d", &k);//Number of digits to move
	int len = strlen(arr);
	rotate(arr, len, k);
	printf("%s", arr);
	return 0;
}

Idea 4: block transformation (inversion)

  • The first two algorithms improve the spatial complexity but reduce the time complexity
  • The third algorithm improves the time complexity but reduces the space complexity
  • In the end, there is an algorithm with relatively low space and time complexity. That must be a drop!
  1. Step 1: flip the first k characters in pairs.
  2. Step 2: flip the last k (including k) characters.
  3. Step 3: flip the whole character.

Relevant diagrams:

Code implementation:

```c
 #include <stdio.h>
#include <string.h>
#include <assert.h>
void reverse(char* left,char* right)
{
	while (left < right)
	{
	char temp = 0;
	temp = *left;
	*left = *right;
	*right = temp;
	left++;
	right--;
	}
}
void rotate(char* arr, int len, int k)
{
	assert(arr != NULL);  //The string array passed cannot be empty
	assert(k <= len);     //The number of moving bits passed must be less than the length of the string
	//Rotate the first k elements
	reverse(arr,  arr + k - 1);
	//K (including k) elements after rotation
	reverse(arr + k , arr + len - 1);
	//Rotate whole element
	reverse(arr, arr + len - 1);
}
int main()
{
	char arr[] = "ABCD";
	int k = 0;
	scanf("%d", &k);//Rotate the character to the left by k bits
	int len = strlen(arr); //Character length
	rotate(arr, len, k);
	printf("%s", arr);
	return 0;
}
 

Topics: Algorithm