-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_String.py
More file actions
29 lines (23 loc) · 809 Bytes
/
Copy pathReverse_String.py
File metadata and controls
29 lines (23 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
File: Reverse_String.py
Description: A program to reverse a string using two-pointer technique
Author: Madhu Kumar
"""
def reverse_string(str)->str:
"""
Reverses a string using two-pointer approach
Args:
str: Input string to be reversed
Returns:
str: Reversed string
"""
left, right = 0, len(str)-1 # Initialize pointers at start and end
chars = list(str) # Convert string to list for in-place swapping
# Swap characters from both ends moving towards center
while left < right:
chars[left], chars[right] = chars[right], chars[left]
left += 1 # Move left pointer right
right -= 1 # Move right pointer left
return "".join(chars) # Convert list back to string
str = "madhu"
print(reverse_string(str))