Roshan Roshan - 25 days ago 5
Java Question

Keep track of character in string in java

Suppose I Have a string as aabc. Now have to move character in string in circular way as

aabc--->abca-->bcaa-->caab


Now as we see that the first character a has been moved to 2nd position.
The problem is that the string can be rotated infinite no. of time but I have to keep track of 1st character.
Is there any way to do this

Answer

The problem is that the string can be rotated infinite no. of time but I have to keep track of 1st character. Is there any way to do this.

In general, no.

Consider the string "XXXX". There is no way to tell the difference between the possible rotations of this string. Therefore, you can't "keep track" of where the first character of the original string went.

Also, consider this string "abcd". There are 4 possible states that can be reached by rotating the string, but there is an infinite number of rotations. You can "keep track" of the first character ('a') ... by calling str.indexOf("a") ... but that doesn't tell you how many rotations were performed. (The final position of the 'a' tells you the total rotation modulus the length of the string. But it can't tell you the total rotation, or the sequence of the individual rotations.)