File size: 1,359 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10047", "name": "LeetCode 0541: Reverse String Ii"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Reverse String II (Difficulty: Easy)\nTags: Two Pointers, String\n\nGiven a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.\nIf there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.\n\nExample 1:\nInput: s = \"abcdefg\", k = 2\nOutput: \"bacdfeg\"\n\nExample 2:\nInput: s = \"abcd\", k = 2\nOutput: \"bacd\"\n\nConstraints:\n- 1 <= s.length <= 10^4\n- s consists of only lowercase English letters.\n- 1 <= k <= 10^4"}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O(n)\n# Space: O(1)\n\nclass Solution(object):\n    def reverseStr(self, s, k):\n        \"\"\"\n        :type s: str\n        :type k: int\n        :rtype: str\n        \"\"\"\n        s = list(s)\n        for i in range(0, len(s), 2*k):\n            s[i:i+k] = reversed(s[i:i+k])\n        return \"\".join(s)\n\n"}}