31 - Rotate string

Back to Problem Solutions forum

shatseli     2020-03-31 04:37:37

Hi Rodion Despite the fact that I completed this task after reading your Author notes I start to fillof myself absolutely unclear about the way of solution in a most "economic" way ( using char by char approach). I have the following questions regarding your notes :

  1. First of all the main question : Why the task called "Rotate" string while actually all that task requiring us is just simply SHIFT a part of string to the end of beginning ( depending on sign of number key value) ?

  2. Please advice why you suggest to REVERT the order of characters in you author notes regarding the solution: I simply cannot see the logic in that why in case "3 abcde" it should be REVERTED FIRST to the sequence of character: "decba" even when this approach moves it char by char ???

  3. Another question : does copying the String to char array and further manipulating of array element will be concermed in term of professional the programming on Java on economic one ?

  4. In task description you use the text "small latin letters". Despite it is clear for everybody I'd rather will suggest to use more correct adjective "lowercase"

Hopefully think my question will be replied because I really apprrecite Your site and tlury want to understand the more professional way to solve this task !

Rodion (admin)     2020-03-31 05:22:55
User avatar

Dear Eli, Hi!

First of all let me clarify: this exercise is a valuable for more "low-level" languages, which has mutable strings, like C (with char[] for string) Pascal or even Fortran. It makes less sense with immutable strings like in Java and Python. The matter is we want to perform operation without using additional memory besides the string container itself (perhaps one or two single value variables are ok).

e.g. your code (and very general) making two substrings and concatenating them is not "in-place" (we seem to create from 1 to 3 new strings depending on implementation).

Why the task called "Rotate" string while actually all that task requiring us is just simply SHIFT

But you see, some part of the string is shifted beyond the end and added from other side. That is the concept of circular buffer and we usually call this rotate rather than shift (when such part is discarded, not added anywhere).

E.g. for abcde shift 2 chars right will give abc, but rotate will give deabc. Another word for it is Circular Shift

Please advice why you suggest to REVERT the order of characters

Because if we try doing it in-place, the problem becomes difficult. Reverting twice (around different centers) is one popular approach (but there are others).

does copying the String to char array and further manipulating of array element will be concermed in term of professional the programming on Java on economic one

Let's add some concretics. Try making array of string, revert this array and make string of it again:

char[] arr = source_str.toCharArray();
...
char c;
int i, j, k;
// some manipulations on arr itself, you are only allowed to use arr, c, i, j, k variables!

String target_str = new String(arr);

This is not relevant (or is orthogonal) to "professional Java programming", as professionally we don't need to solve such problems. It's rather logic puzzle about more low-level algorithm.

it is clear for everybody I'd rather will suggest to use more correct adjective "lowercase"

Yeah, thanks, that's correct, let's change it to "lowercase". You may see I'm not guru in English, and when I started the site my English was even more poor - so there are still mistakes and flaws :)

P.S. By the way, please note that you still feel bit too relaxed about following Java Coding Style guidelines in naming variables for example! Also it would be good to separate the code of reverting string into dedicated function, for example :)

shatseli     2020-03-31 18:50:43

Dear Rodion Thanks a lot for your quick responce and detailed information ! You site is really GREAT !

Please login and solve 5 problems to be able to post at forum