Skip to content

Latest commit

 

History

History
29 lines (20 loc) · 548 Bytes

File metadata and controls

29 lines (20 loc) · 548 Bytes

541 Reverse String II

Description

link


Solution

  • See Code

Code

O(n)

class Solution:
    def reverseStr(self, s: str, k: int) -> str:
        # Divide s into an array of substrings length k
        s = [s[i:i+k] for i in range(0, len(s), k)]
		# Reverse every other substring, beginning with s[0]
        for i in range(0, len(s), 2):
            s[i] = s[i][::-1]
		# Join array of substrings into one string and return 
        return ''.join(s)