Skip to content

Commit 774bd99

Browse files
committed
Runtime: 96 ms (Top 61.23%) | Memory: 14 MB (Top 75.15%)
1 parent 4f9f282 commit 774bd99

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
1+
# Runtime: 96 ms (Top 61.23%) | Memory: 14 MB (Top 75.15%)
12
class Solution:
23
def convert(self, s: str, numRows: int) -> str:
3-
4+
45
# safety check to not process single row
56
if numRows == 1:
67
return s
7-
8+
89
# safety check to not process strings shorter/equal than numRows
910
if len(s) <= numRows:
1011
return s
11-
12+
1213
# safety check to not process double rows
1314
if numRows == 2:
1415
# slice every other character
1516
return s[0::2] + s[1::2]
16-
17+
1718
# list that stores the lines
1819
# add lines with initial letters
1920
lines: list[str] = [letter for letter in s[:numRows]]
20-
21+
2122
# positive direction goes down
2223
# lines are created, so it's going up
2324
direction: int = -1
24-
25+
2526
# track the position at which the letter will be added
2627
# position after bouncing off, after adding initial lines
2728
line_index: int = numRows - 2
28-
29+
2930
# edge indexes
3031
# 0 can only be reached by going up
3132
# numRows only by going down
3233
edges: set[int] = {0, numRows}
33-
34+
3435
for letter in s[numRows:]:
3536
# add letter at tracked index position
3637
lines[line_index] += letter
37-
38+
3839
# prepare index before next loop iteration
3940
line_index += direction
40-
41+
4142
# reaching one of the edges
4243
if line_index in edges:
4344
# change direction
4445
direction = -direction
4546
# bounce off if bottom edge
4647
if line_index == numRows:
4748
line_index += direction * 2
48-
49-
return "".join(lines)
49+
50+
return "".join(lines)

0 commit comments

Comments
 (0)