Skip to content

Commit 33a25e0

Browse files
committed
reverse-string
1 parent 5164054 commit 33a25e0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Two-pointers/easy/revrse-string.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"Leetcode- https://leetcode.com/problems/reverse-string/ "
2+
'''
3+
Write a function that reverses a string. The input string is given as an array of characters s.
4+
5+
You must do this by modifying the input array in-place with O(1) extra memory.
6+
7+
Example 1:
8+
9+
Input: s = ["h","e","l","l","o"]
10+
Output: ["o","l","l","e","h"]
11+
'''
12+
def reverseString(s):
13+
i=0
14+
j=len(s)-1
15+
16+
while i<=j:
17+
s[i],s[j]=s[j],s[i]
18+
i,j = i+1,j-1
19+
20+
return s
21+
22+
#T:O(N)
23+
#S:O(1)

0 commit comments

Comments
 (0)