Reverse String
LeetCode 344 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু
Swap s[i] with s[n−1−i] for i < n/2.
আমি Reverse String (LeetCode 344) এভাবে সমাধান করেছি — Two Pointers প্যাটার্নের একটি সহজ প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।
নিচের বিবরণ ইংরেজিতে।
Intuition
Swap the first and last characters, then the second and second-to-last, and so on until the middle. Each swap fixes two positions.
Approach
- 1
Loop i over the first half.
- 2
Swap s[i] and s[n−i−1].
আমি কীভাবে সমাধান করেছি
Two-ended swapPythonO(n) time · O(1) space
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
n=len(s)
for i in range(n//2):
s[i],s[n-i-1]=s[n-i-1],s[i]
return s
Reverse String — সাধারণ প্রশ্ন
Reverse String-এর পেছনে intuition কী?
Swap the first and last characters, then the second and second-to-last, and so on until the middle. Each swap fixes two positions.
Reverse String (LeetCode 344) কীভাবে সমাধান করবেন?
1. Loop i over the first half. 2. Swap s[i] and s[n−i−1].
Reverse String-এর time আর space complexity কত?
Time: O(n)। Space: O(1)। (Accepted approach: Two-ended swap।)
Reverse String-এ সাধারণ ভুলগুলো কী?
Stop at n // 2 or you'll swap everything back.