-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotateString.py
33 lines (30 loc) · 948 Bytes
/
rotateString.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Given a string and an offset, rotate string by offset. routate from left to right.
# Given 'abcdefg'
# offset=0 => 'abcdefg'
# offset=1 => 'gabcdef'
# offset=2 => 'fgabcde'
# offset=3 => 'efgabcd'
def rotate(str, offset):
assert offset >= 0
if offset > len(str):
offset = offset % len(str)
doubledStr = str * 2
start = len(str) - offset
end = len(str) - offset + len(str)
result = doubledStr[start:end]
return result
print rotate('abc', 1)
print rotate('abcdef', 13)
# @parama: str: an array of char # list
# @parama: offset: an integer
# @return: nothing
def rotateTheStr(str, offset):
assert offset >= 0
if offset > len(str):
offset = offset % len(str)
start = len(str) - offset
end = 2 * len(str) - offset
result = (str * 2)[start:end]
# str = result # wrong on lintcode because of the reference thing
for index in xrange(len(str)):
str[index] = result[index]