-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg006_zigzag.py
41 lines (35 loc) · 1.4 KB
/
alg006_zigzag.py
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
# 6. Zig Zag Conversation (Medium)
# https://leetcode.com/problems/zigzag-conversion/
#
# Problem:
# The string "PAYPALISHIRING" is written in a zigzag pattern (DOWN - DIAGONAL UP.RIGHT - DOWN ...)
# on a given number of rows like this string convert(string s, int numRows)
#
# Example:
# DOWN - DIAGONAL UP.RIGHT - DOWN ...
# Input: "PAYPALISHIRING"
# Output: line by line: "PAHNAPLSIIGYIR"
# Explanation:
# P A H N
# A P L S I I G
# Y I R
class Solution:
def convert(self, s: str, numRows: int) -> str:
lines = [[] for i in range(numRows)]
length = len(s)
# Get Order of Lines for One Loop of Steps (Down, Diagonal Up-Right)
lnmap = list(range(numRows)) + list(range(numRows - 2, 0, -1))
for i, c in enumerate(s):
l = i % len(lnmap) # Find Loop Iteration of Character Index
lines[lnmap[l]].append(c) # Append Character to proper Line
# Create Re-ordered String of Lines
r = ''
for l in lines:
r += ''.join(l)
return r
# ===============================================================================
# ===============================================================================
# ===============================================================================
if __name__ == "__main__":
sol = Solution()
print(sol.convert("PAYPALISHIRING", 3))