-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag_conversion.py
More file actions
73 lines (61 loc) · 2.07 KB
/
zigzag_conversion.py
File metadata and controls
73 lines (61 loc) · 2.07 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
64
65
66
67
68
69
70
71
72
73
"""
6. Zigzag Conversion
Time Complexity: O(n) where n is length of string
Space Complexity: O(n) for storing rows
"""
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1 or numRows >= len(s):
return s
rows = [''] * numRows
current_row = 0
going_down = False
for char in s:
rows[current_row] += char
if current_row == 0 or current_row == numRows - 1:
going_down = not going_down
current_row += 1 if going_down else -1
return ''.join(rows)
# Test cases
if __name__ == "__main__":
solution = Solution()
# Example 1
s1 = "PAYPALISHIRING"
numRows1 = 3
print(f"Example 1: {solution.convert(s1, numRows1)}") # Expected: "PAHNAPLSIIGYIR"
# Example 2
s2 = "PAYPALISHIRING"
numRows2 = 4
print(f"Example 2: {solution.convert(s2, numRows2)}") # Expected: "PINALSIGYAHRPI"
# Example 3
s3 = "A"
numRows3 = 1
print(f"Example 3: {solution.convert(s3, numRows3)}") # Expected: "A"
# Edge case: numRows = 2
s4 = "ABCDE"
numRows4 = 2
print(f"Example 4: {solution.convert(s4, numRows4)}") # Expected: "ACEBD"
# Pattern: A C E
# B D
# Edge case: numRows >= len(s)
s5 = "ABC"
numRows5 = 5
print(f"Example 5: {solution.convert(s5, numRows5)}") # Expected: "ABC"
# Test with numRows = 5
s6 = "ABCDEFGHIJKLMNO"
numRows6 = 5
print(f"Example 6: {solution.convert(s6, numRows6)}")
# Pattern: A I
# B H J N
# C G K M O
# D F L
# E
# Expected: "AIBHJNC GKMODEFL"
# Test mathematical approach
s7 = "PAYPALISHIRING"
numRows7 = 3
print(f"Example 7 (mathematical): {solution.convertMathematical(s7, numRows7)}") # Expected: "PAHNAPLSIIGYIR"
# Test mathematical approach with numRows = 4
s8 = "PAYPALISHIRING"
numRows8 = 4
print(f"Example 8 (mathematical): {solution.convertMathematical(s8, numRows8)}") # Expected: "PINALSIGYAHRPI"