-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
63 lines (49 loc) · 1.73 KB
/
__init__.py
File metadata and controls
63 lines (49 loc) · 1.73 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import List
from datastructures.stacks.dynamic import DynamicSizeStack as Stack
def reverse_string(text: str) -> str:
"""
Reverses an input string using a stack data structure
Complexity:
Where n is the length of the input string
Time: O(n), iterate through the whole string
Space: O(n), a stack is used to store the characters in the input string.
Args:
text (str): string to reverse
Return:
str: reversed string
"""
# initialize an empty stack
stack = Stack()
# push all the characters onto the stack
for char in text:
stack.push(char)
# initialize an empty string which will be the result
reversed_string = ""
# iterate over all the items in the stack popping of items from the top of the stack and adding them to the result
# string.
while not stack.is_empty():
reversed_string += stack.pop()
return reversed_string
def reverse_string_char_array(s: List[str]) -> None:
"""
Reverses a character array in place using two-pointer technique.
Reference: https://leetcode.com/problems/reverse-string/
Complexity:
Where n is the length of the input list
Time: O(n), each character is visited at most once
Space: O(1), only two pointers used, no extra data structures
Args:
s (List[str]): character array to reverse in place
Returns:
None: modifies the input list in place
"""
if not s or len(s) == 1:
return
# Initialize two pointers: left at start, right at end
left = 0
right = len(s) - 1
# Swap characters while left is before right
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1