Rotate the String

2 views
Skip to first unread message

gaurav gupta

unread,
Aug 16, 2009, 3:16:29 AM8/16/09
to DS & Algo@itbhu
You are given an array with n elements { x1, x2 ... xk ... xn }, and K;

Rotate this string about K in O(n) time and constant space.

--
GAURAV GUPTA
B.Tech IV Yr. , Department of Computer Science & Engineering
IT BHU , Varanasi
Contacts
Phone No: +91-99569-49491

e-mail :
gaurav...@acm.org
gaurav.gu...@itbhu.ac.in

Neeraj Singh

unread,
Aug 16, 2009, 3:49:08 AM8/16/09
to ds--al...@googlegroups.com
On Sun, Aug 16, 2009 at 12:46 PM, gaurav gupta <1989....@googlemail.com> wrote:
You are given an array with n elements { x1, x2 ... xk ... xn }, and K;

Rotate this string about K in O(n) time and constant space.
what is the final array after rotation.

--
GAURAV GUPTA
B.Tech IV Yr. , Department of Computer Science & Engineering
IT BHU , Varanasi
Contacts
Phone No: +91-99569-49491

e-mail :
gaurav...@acm.org
gaurav.gu...@itbhu.ac.in





--
Neeraj Kumar
B.Tech (Part III)
Department of Computer Sc. & Engineering
IT-BHU,Varanasi

Ajay

unread,
Aug 16, 2009, 3:56:52 AM8/16/09
to ds--al...@googlegroups.com
three reverse are required for this.

  1. Reverse the whole string.
  2. Now count K from last.
  3. Reverse the first half (1 to n-k+1)
  4. reverse 2nd half (n-k+2 to n).
for Ex-

ABCDEF,  n=6
rotate around D (K=4)
  • 1st rotation FEDCBA
  • Pivot is 6-4+1=3
  • Rev FED
  • Rev CBA
You will get
DEFABC
thats what you want.

On Sun, Aug 16, 2009 at 12:46 PM, gaurav gupta <1989....@googlemail.com> wrote:



--
Ajay Kr. Gautam
Postgraduate Student,
Dept. of Computer Science & Engg., IT-BHU
Varanasi-221005, UP
India
-----------------------------------------------------------------
"Easy reading is damned hard writing."
~ Nathaniel Hawthorne
Reply all
Reply to author
Forward
0 new messages