# 1 line simple recursive Python

• ``````class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
return s[:k][::-1] + s[k:2*k] + self.reverseStr(s[2*k:], k) if s else ""
``````

• Very elegant!

• help! what does it means when it follow the recursive part?

if s else ""

• help! what does it means when it follow the recursive part?

It means when current `s` is empty string, just return an empty string. One thing to keep in mind is during the recursion, current `s` is changing since we did some slicing in previous step.

• @realisking
Cool! So it means that the if-else statement acts on the argument s of the function.I just do not know this grammar because i just learned python last week.
cheers!

• @realisking
BTW，if i put this if-else statement In the body of the function, it still work the same, right?

• @realisking
Many thanks.

• thanks, very concise and elegant solution

• class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
return s[:k][::-1] + s[k:2k] + self.reverseStr(s[2k:], k) if s else ""

Same as yours, except that I don't do recursion :P

``````class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
if k >= len(s):
return s[::-1]
for i in range(0, len(s), 2 * k):
s = s[:i]+s[i:i+k][::-1] + s[i+k:]
return s
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.