For every block of 2k characters starting with position i, we want to replace S[i:i+k] with it's reverse.
def reverseStr(self, s, k): s = list(s) for i in xrange(0, len(s), 2*k): s[i:i+k] = reversed(s[i:i+k]) return "".join(s)
@awice Nice job man. I did in 40 lines of code what you did in 4. T_T
Why is there no need to add 'try-except' blocks?. As there might be case that i+k may go beyond length of string.
In Python, slices are handled safely with respect to indices that are out of bounds.
@awice Is this always the case?. I mean, should I never think if the array is going out of bounds when using slicing operations?.
@erickvs me too...
@erickvs are you serious?40 lines of python? if I wrote more than twenty lines of code,I would like to think whether the question is wrong .lol :]
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.