Javatpoint Logo
Javatpoint Logo

An In-Place Algorithm for String Transformation in C++

In this article, we will discuss an In-Place Algorithm for string transformation in C++ with several examples.

In this algorithm, transfer all items with even positions for a given string to the end of the string. Maintain the same relative order for all elements that are placed even and odd while transferring them.

For instance, transform a string into "abcdefghijklm1234567891234" in-place and with O(n) time complexity if it is "a1b2c3d4e5f6g7h8i9j1k2l3m4".

These are the steps:

Remove the largest size prefix substring of the form 3k + 1. Finding the greatest non-negative number k such that 3k+1 is less than or equal to n (length of string) is what we do in this phase.

  • Implement the cycle leader iteration technique, starting with the index 1, 3, 9, etc., to this substring. All the elements of this substring are relocated to their proper locations using the cycle leader iteration technique, which implies that all the alphabets are moved to the left half of the substring and all the digits are transferred to the right half.
  • Applying the first and second stages, process the remaining substring iteratively.
  • We currently simply need to combine the processed sub-strings. Start at any end (let's say the left), choose two substrings, and execute the following actions:
  • Simply flip or reverse the first sub-string's second half.
  • Simply invert or oppose the first half of the second sub-string.
  • Just combine the first half of the second substring with the second half of the first substring to oppose or reverse them.
  • Until and unless all sub-strings are linked, repeat step no. 4. It is the same as k-way merging, where the first and second substrings are linked. The outcome is combined with the third, and so forth.

Example:

Let's use an example to better understand it:

Please take note that the values used in the example below include 10, 11, and 12. Use only these values as single characters. For better readability, these values are utilized.

  • Two substrings of size 10 each are created after breaking into the size of the form 3k+1. Sizes 4 and 2 are used to create the third and fourth sub-strings, respectively.
  • Following the first sub-string's use of the cycle leader iteration algorithm:
  • When the second sub-string is subject to the cycle leader iteration algorithm:
  • When the third sub-string is subject to the cycle leader iteration algorithm:
  • When the fourth sub-string is subject to the cycle leader iteration algorithm:

Combining the first and second substrings:

1. The initial half of the second substring and the second half of the first substring are inverted.

2. The first half of the second substring and the second half of the first substring, both reversed (they are combined, so there are now only three substrings).

Again, combining the first and second substrings:

1. The initial half of the second substring and the second half of the first substring are inverted.

2. The first half of the second substring and the second half of the first substring, both reversed, combined (they are merged, so there are now just two substrings).

Linking the first and second substrings:

1. The initial half of the second substring and the second half of the first substring are inverted.

2. The first half of the second substring and the second half of the first substring, both reversed, combined (they are now one substring).

The code is shown below based on the algorithm above:

Example 1:

Output:

oellHWrldo

Example: 2

Let us take another example to illustrate the In-Place algorithm for String Transformation in C++.

Output:

sihT si a elpmas ecnetnes ot esrever sdrow

Important Points:

  • If the array size is already in the form 3k+1, the cycle leader iteration algorithm can be used right away. There is no requirement to join.
  • Only arrays with a size of the form 3k + 1 can use the cycle leader iteration technique.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA