From 9dce797fa5538861f89f9df94b0f4a7883a9da64 Mon Sep 17 00:00:00 2001 From: leonardchinonso <36096513+leonardchinonso@users.noreply.github.com> Date: Sat, 17 Apr 2021 01:09:31 +0100 Subject: [PATCH] Update 06.py --- 1-100q/06.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/1-100q/06.py b/1-100q/06.py index 8f3c50c..da2f6d8 100644 --- a/1-100q/06.py +++ b/1-100q/06.py @@ -38,4 +38,23 @@ def convert(self, s, numRows): final_string += value return final_string -print Solution().convert("PAYPALISHIRING",3) \ No newline at end of file +print Solution().convert("PAYPALISHIRING",3) + + +''' +Alternative Solution +''' + +class Solution: + def convert(self, s: str, numRows: int) -> str: + if numRows == 1: return s + out = [[] for _ in range(numRows)] + goingDown = False + nextt = 0 + for char in s: + out[nextt].append(char) + if nextt == 0 or nextt == numRows - 1: + goingDown = not goingDown + if not goingDown: nextt += 1 + else: nextt -= 1 + return ''.join(["".join(arr) for arr in out])