Last updated on January 20th, 2025 at 04:16 am
Here, we see the Zigzag Conversion LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
String
Level of Question
Medium
Zigzag Conversion LeetCode Solution
Table of Contents
1. Problem Statement
The string PAYPALISHIRING is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR"
Example 1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2: Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation: P I N A L S I G Y A H R P I Example 3: Input: s = "A", numRows = 1 Output: "A"
2. Coding Pattern Used in Solution
The coding pattern used in the provided code is “Zigzag Conversion”. This pattern involves arranging characters in a zigzag pattern across multiple rows and then reading them row by row to form the final string. It is a specialized problem-solving approach that doesn’t directly map to the common patterns listed.
3. Code Implementation in Different Languages
3.1 Zigzag Conversion C++
class Solution { public: string convert(string s, int numRows) { if (numRows == 1) return s; string ret; int n = s.size(); int cycleLen = 2 * numRows - 2; for (int i = 0; i < numRows; i++) { for (int j = 0; j + i < n; j += cycleLen) { ret += s[j + i]; if (i != 0 && i != numRows - 1 && j + cycleLen - i < n) ret += s[j + cycleLen - i]; } } return ret; } };
3.2 Zigzag Conversion Java
class Solution { public String convert(String s, int numRows) { if (numRows == 1) return s; StringBuilder ret = new StringBuilder(); int n = s.length(); int cycleLen = 2 * numRows - 2; for (int i = 0; i < numRows; i++) { for (int j = 0; j + i < n; j += cycleLen) { ret.append(s.charAt(j + i)); if (i != 0 && i != numRows - 1 && j + cycleLen - i < n) ret.append(s.charAt(j + cycleLen - i)); } } return ret.toString(); } }
3.3 Zigzag Conversion JavaScript
var convert = function(s, numRows) { if (numRows === 1 || s.length < numRows) return s; let rows = []; let converted = ''; let reverse = false; let count = 0; for (let i = 0; i < numRows; i++) rows[i] = []; for (let i = 0; i < s.length; i++) { rows[count].push(s[i]); reverse ? count-- : count++; if (count === numRows - 1 || count === 0) reverse = !reverse; } return rows.reduce((converted, cur) => converted + cur.join(''), ''); };
3.4 Zigzag Conversion Python
class Solution: def convert(self, s: str, numRows: int) -> str: if numRows == 1: return s row_arr = [""] * numRows row_idx = 1 going_up = True for ch in s: row_arr[row_idx-1] += ch if row_idx == numRows: going_up = False elif row_idx == 1: going_up = True if going_up: row_idx += 1 else: row_idx -= 1 return "".join(row_arr)
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n) | O(n) |
Java | O(n) | O(n) |
JavaScript | O(n) | O(n) |
Python | O(n) | O(n) |
- The code implements a Zigzag Conversion pattern with a time complexity of O(n) and a space complexity of O(n) across all languages.
- It efficiently processes the input string to construct the zigzag pattern row by row.