Counting cyclic shifts of a string

356 views Asked by At

I need to write a function that will return the number of possible different cyclic shifts of an input string.

Could you please give me some tips on where should I start in order to create an efficient (in terms of time complexity) algorithm? Should I begin with "preprocessing" the string and creating some data structure to help count the shifts later?

1

There are 1 answers

0
Paul Evans On

Simply store the string consecutively twice somewhere e.g.:

"this_is_my_longish_stringthis_is_my_longish_string"
     ^                       ^
     |                       |
     |<-string length apart->|

and then just move two index's from beginning to end of the first string "string length apart" along that double string, returning the interim string each time. Alternatively, you can work on your homework problems yourself.