<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Aryan Subudhi</title>
    <description>The latest articles on Forem by Aryan Subudhi (@aryan_subudhi_01).</description>
    <link>https://forem.com/aryan_subudhi_01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3949061%2Fcccd6479-33a2-4da7-be7e-f75607518d4d.png</url>
      <title>Forem: Aryan Subudhi</title>
      <link>https://forem.com/aryan_subudhi_01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aryan_subudhi_01"/>
    <language>en</language>
    <item>
      <title>LeetCode Solution: 6. Zigzag Conversion</title>
      <dc:creator>Aryan Subudhi</dc:creator>
      <pubDate>Sun, 24 May 2026 13:03:49 +0000</pubDate>
      <link>https://forem.com/aryan_subudhi_01/leetcode-solution-6-zigzag-conversion-eln</link>
      <guid>https://forem.com/aryan_subudhi_01/leetcode-solution-6-zigzag-conversion-eln</guid>
      <description>&lt;h1&gt;
  
  
  Unraveling the Zigzag: A Beginner's Guide to LeetCode 6 (Zigzag Conversion)
&lt;/h1&gt;

&lt;p&gt;Hey LeetCoders and aspiring competitive programmers! aryan-subudhi here, ready to dive into another fascinating problem that looks tricky on the surface but unlocks a cool pattern-recognition skill. Today, we're tackling &lt;strong&gt;LeetCode Problem 6: Zigzag Conversion&lt;/strong&gt;. Get ready to bend your string into some interesting shapes!&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Explanation
&lt;/h2&gt;

&lt;p&gt;Imagine you're writing a message, but instead of straight lines, you write it in a zigzag pattern across a given number of rows. Once you've written it, you then read the message line by line, from left to right. Your task is to implement a function &lt;code&gt;convert&lt;/code&gt; that takes a string &lt;code&gt;s&lt;/code&gt; and an integer &lt;code&gt;numRows&lt;/code&gt; and returns the converted string.&lt;/p&gt;

&lt;p&gt;Let's look at an example. If our input string is &lt;code&gt;s = "PAYPALISHIRING"&lt;/code&gt; and &lt;code&gt;numRows = 3&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;We write it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;P   A   H   N
A P L S I I G
Y   I   R
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how &lt;code&gt;P&lt;/code&gt; is in row 0, &lt;code&gt;A&lt;/code&gt; in row 1, &lt;code&gt;Y&lt;/code&gt; in row 2, then we go back up with &lt;code&gt;P&lt;/code&gt; in row 1, &lt;code&gt;A&lt;/code&gt; in row 0, and so on. It forms a 'N' or 'Z' shape.&lt;/p&gt;

&lt;p&gt;Once written, we read it line by line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Row 0: &lt;code&gt;P&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;H&lt;/code&gt;, &lt;code&gt;N&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Row 1: &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;P&lt;/code&gt;, &lt;code&gt;L&lt;/code&gt;, &lt;code&gt;S&lt;/code&gt;, &lt;code&gt;I&lt;/code&gt;, &lt;code&gt;I&lt;/code&gt;, &lt;code&gt;G&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Row 2: &lt;code&gt;Y&lt;/code&gt;, &lt;code&gt;I&lt;/code&gt;, &lt;code&gt;R&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Concatenating these gives us: &lt;code&gt;"PAHNAPLSIIGYIR"&lt;/code&gt;. This is our expected output!&lt;/p&gt;

&lt;p&gt;Another example: &lt;code&gt;s = "PAYPALISHIRING"&lt;/code&gt;, &lt;code&gt;numRows = 4&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;P     I    N
A   L S  I G
Y A   H R
P     I
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reading line by line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Row 0: &lt;code&gt;P&lt;/code&gt;, &lt;code&gt;I&lt;/code&gt;, &lt;code&gt;N&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Row 1: &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;L&lt;/code&gt;, &lt;code&gt;S&lt;/code&gt;, &lt;code&gt;I&lt;/code&gt;, &lt;code&gt;G&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Row 2: &lt;code&gt;Y&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;H&lt;/code&gt;, &lt;code&gt;R&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Row 3: &lt;code&gt;P&lt;/code&gt;, &lt;code&gt;I&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Concatenating: &lt;code&gt;"PINALSIGYAHRPI"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The string length &lt;code&gt;s&lt;/code&gt; can be up to 1000 characters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s&lt;/code&gt; consists of English letters (upper/lower-case), ',', and '.'.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;numRows&lt;/code&gt; can be between 1 and 1000.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An important edge case: if &lt;code&gt;numRows = 1&lt;/code&gt;, the zigzag pattern doesn't really form. The string remains unchanged. &lt;code&gt;convert("A", 1)&lt;/code&gt; should return &lt;code&gt;"A"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Intuition
&lt;/h2&gt;

&lt;p&gt;At its core, this problem is about simulating the pattern. If we were doing this manually, we'd pick a character from the input string, place it in the correct row, then decide where the &lt;em&gt;next&lt;/em&gt; character goes. This suggests we need a way to keep track of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Which row we're currently placing characters into.&lt;/li&gt;
&lt;li&gt;The direction we're moving (downwards or upwards).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The pattern repeats in cycles: go down from &lt;code&gt;row 0&lt;/code&gt; to &lt;code&gt;numRows - 1&lt;/code&gt;, then go up from &lt;code&gt;numRows - 2&lt;/code&gt; to &lt;code&gt;row 1&lt;/code&gt;, and then repeat. Notice that &lt;code&gt;row 0&lt;/code&gt; and &lt;code&gt;numRows - 1&lt;/code&gt; are only hit when going "down." The "up" phase skips these two rows to avoid double-counting or overshooting the pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach: Simulating the Zigzag
&lt;/h2&gt;

&lt;p&gt;Let's formalize our intuition into a concrete algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handle the Edge Case:&lt;/strong&gt; If &lt;code&gt;numRows&lt;/code&gt; is 1, no zigzag conversion is needed. Simply return the original string &lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize Rows:&lt;/strong&gt; We need a way to store characters for each row. A list of lists (or list of strings/string builders) is perfect for this. Let's create &lt;code&gt;numRows&lt;/code&gt; empty lists. For example, &lt;code&gt;mat = [[] for _ in range(numRows)]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Traverse and Populate:&lt;/strong&gt; We'll iterate through the input string &lt;code&gt;s&lt;/code&gt; character by character. For each character, we need to append it to the correct row. The key is to manage our &lt;code&gt;current_row&lt;/code&gt; index and the &lt;code&gt;direction&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The pattern can be seen as a series of "full columns" going down, followed by "diagonal segments" going up-right.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Phase 1: Going Down:&lt;/strong&gt; We start at &lt;code&gt;row 0&lt;/code&gt; and append characters sequentially to &lt;code&gt;mat[0]&lt;/code&gt;, &lt;code&gt;mat[1]&lt;/code&gt;, ..., up to &lt;code&gt;mat[numRows - 1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Phase 2: Going Up-Right:&lt;/strong&gt; After reaching the bottom, we move "up and right." This means we append characters to &lt;code&gt;mat[numRows - 2]&lt;/code&gt;, &lt;code&gt;mat[numRows - 3]&lt;/code&gt;, ..., up to &lt;code&gt;mat[1]&lt;/code&gt;. We stop before &lt;code&gt;row 0&lt;/code&gt; because &lt;code&gt;row 0&lt;/code&gt; is the start of the next "down" phase. Similarly, &lt;code&gt;numRows - 1&lt;/code&gt; is only hit when going down.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We repeat these two phases until all characters from &lt;code&gt;s&lt;/code&gt; are processed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Construct Result:&lt;/strong&gt; Once all characters have been placed into their respective row lists, concatenate all the lists (or strings) in &lt;code&gt;mat&lt;/code&gt; from &lt;code&gt;row 0&lt;/code&gt; to &lt;code&gt;numRows - 1&lt;/code&gt; to form the final result string.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's trace &lt;code&gt;s = "PAYPALISHIRING", numRows = 3&lt;/code&gt; with this approach:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mat = [[], [], []]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;s_idx = 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cycle 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Down Phase:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;s[0]&lt;/code&gt; ('P') -&amp;gt; &lt;code&gt;mat[0]&lt;/code&gt; -&amp;gt; &lt;code&gt;['P']&lt;/code&gt;, &lt;code&gt;s_idx = 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s[1]&lt;/code&gt; ('A') -&amp;gt; &lt;code&gt;mat[1]&lt;/code&gt; -&amp;gt; &lt;code&gt;['A']&lt;/code&gt;, &lt;code&gt;s_idx = 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s[2]&lt;/code&gt; ('Y') -&amp;gt; &lt;code&gt;mat[2]&lt;/code&gt; -&amp;gt; &lt;code&gt;['Y']&lt;/code&gt;, &lt;code&gt;s_idx = 3&lt;/code&gt; (Reached &lt;code&gt;numRows - 1&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Up-Right Phase:&lt;/strong&gt; (&lt;code&gt;numRows - 2&lt;/code&gt; down to &lt;code&gt;1&lt;/code&gt; -&amp;gt; &lt;code&gt;3 - 2 = 1&lt;/code&gt; down to &lt;code&gt;1&lt;/code&gt;)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;s[3]&lt;/code&gt; ('P') -&amp;gt; &lt;code&gt;mat[1]&lt;/code&gt; -&amp;gt; &lt;code&gt;['A', 'P']&lt;/code&gt;, &lt;code&gt;s_idx = 4&lt;/code&gt; (Reached &lt;code&gt;row 1&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cycle 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Down Phase:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;s[4]&lt;/code&gt; ('A') -&amp;gt; &lt;code&gt;mat[0]&lt;/code&gt; -&amp;gt; &lt;code&gt;['P', 'A']&lt;/code&gt;, &lt;code&gt;s_idx = 5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s[5]&lt;/code&gt; ('L') -&amp;gt; &lt;code&gt;mat[1]&lt;/code&gt; -&amp;gt; &lt;code&gt;['A', 'P', 'L']&lt;/code&gt;, &lt;code&gt;s_idx = 6&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s[6]&lt;/code&gt; ('I') -&amp;gt; &lt;code&gt;mat[2]&lt;/code&gt; -&amp;gt; &lt;code&gt;['Y', 'I']&lt;/code&gt;, &lt;code&gt;s_idx = 7&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Up-Right Phase:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;s[7]&lt;/code&gt; ('S') -&amp;gt; &lt;code&gt;mat[1]&lt;/code&gt; -&amp;gt; &lt;code&gt;['A', 'P', 'L', 'S']&lt;/code&gt;, &lt;code&gt;s_idx = 8&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cycle 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Down Phase:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;s[8]&lt;/code&gt; ('H') -&amp;gt; &lt;code&gt;mat[0]&lt;/code&gt; -&amp;gt; &lt;code&gt;['P', 'A', 'H']&lt;/code&gt;, &lt;code&gt;s_idx = 9&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s[9]&lt;/code&gt; ('I') -&amp;gt; &lt;code&gt;mat[1]&lt;/code&gt; -&amp;gt; &lt;code&gt;['A', 'P', 'L', 'S', 'I']&lt;/code&gt;, &lt;code&gt;s_idx = 10&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s[10]&lt;/code&gt; ('R') -&amp;gt; &lt;code&gt;mat[2]&lt;/code&gt; -&amp;gt; &lt;code&gt;['Y', 'I', 'R']&lt;/code&gt;, &lt;code&gt;s_idx = 11&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Up-Right Phase:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;s[11]&lt;/code&gt; ('I') -&amp;gt; &lt;code&gt;mat[1]&lt;/code&gt; -&amp;gt; &lt;code&gt;['A', 'P', 'L', 'S', 'I', 'I']&lt;/code&gt;, &lt;code&gt;s_idx = 12&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cycle 4:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Down Phase:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;s[12]&lt;/code&gt; ('N') -&amp;gt; &lt;code&gt;mat[0]&lt;/code&gt; -&amp;gt; &lt;code&gt;['P', 'A', 'H', 'N']&lt;/code&gt;, &lt;code&gt;s_idx = 13&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s[13]&lt;/code&gt; ('G') -&amp;gt; &lt;code&gt;mat[1]&lt;/code&gt; -&amp;gt; &lt;code&gt;['A', 'P', 'L', 'S', 'I', 'I', 'G']&lt;/code&gt;, &lt;code&gt;s_idx = 14&lt;/code&gt; (String &lt;code&gt;s&lt;/code&gt; exhausted)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Final &lt;code&gt;mat&lt;/code&gt;:&lt;br&gt;
&lt;code&gt;mat[0] = ['P', 'A', 'H', 'N']&lt;/code&gt;&lt;br&gt;
&lt;code&gt;mat[1] = ['A', 'P', 'L', 'S', 'I', 'I', 'G']&lt;/code&gt;&lt;br&gt;
&lt;code&gt;mat[2] = ['Y', 'I', 'R']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Join them: "PAHN" + "APLSIIG" + "YIR" = "PAHNAPLSIIGYIR". Perfect!&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numRows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Edge case: If there's only one row, no zigzag conversion needed.
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;numRows&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;

        &lt;span class="c1"&gt;# Initialize numRows empty lists to represent each row.
&lt;/span&gt;        &lt;span class="c1"&gt;# Each list will store characters that belong to that row.
&lt;/span&gt;        &lt;span class="n"&gt;mat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numRows&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

        &lt;span class="c1"&gt;# Pointer for the current character in the input string s.
&lt;/span&gt;        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="c1"&gt;# Length of the input string s.
&lt;/span&gt;        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# Loop until all characters from the input string are processed.
&lt;/span&gt;        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Phase 1: Go down (from row 0 to numRows - 1)
&lt;/span&gt;            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numRows&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Check if there are still characters left in s
&lt;/span&gt;                    &lt;span class="n"&gt;mat&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;down&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
                    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

            &lt;span class="c1"&gt;# Phase 2: Go up-right (from numRows - 2 down to row 1)
&lt;/span&gt;            &lt;span class="c1"&gt;# We skip row 0 and numRows - 1 as they are handled exclusively by the 'down' phase
&lt;/span&gt;            &lt;span class="c1"&gt;# or will be the start/end of the next 'down' phase.
&lt;/span&gt;            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numRows&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Check if there are still characters left in s
&lt;/span&gt;                    &lt;span class="n"&gt;mat&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
                    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="c1"&gt;# After populating all rows, join the characters in each row
&lt;/span&gt;        &lt;span class="c1"&gt;# and then concatenate all rows to form the final result string.
&lt;/span&gt;        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;mat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Use extend for efficiency, then join once
&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Time &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We iterate through the input string &lt;code&gt;s&lt;/code&gt; once using the &lt;code&gt;while i &amp;lt; n&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;Inside the loop, each character &lt;code&gt;s[i]&lt;/code&gt; is appended to a list in &lt;code&gt;mat&lt;/code&gt; exactly once.&lt;/li&gt;
&lt;li&gt;Finally, we iterate through the &lt;code&gt;mat&lt;/code&gt; (which collectively holds &lt;code&gt;N&lt;/code&gt; characters) to build the &lt;code&gt;result&lt;/code&gt; list and then join them into a string. The &lt;code&gt;extend&lt;/code&gt; and &lt;code&gt;join&lt;/code&gt; operations are proportional to the total number of characters, which is &lt;code&gt;N&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Therefore, the overall time complexity is linear with respect to the length of the string &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity: O(N)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We create &lt;code&gt;numRows&lt;/code&gt; lists in &lt;code&gt;mat&lt;/code&gt;. In the worst case (e.g., &lt;code&gt;numRows = N&lt;/code&gt;), each list holds one character. In general, all &lt;code&gt;N&lt;/code&gt; characters of the string &lt;code&gt;s&lt;/code&gt; are stored across these &lt;code&gt;numRows&lt;/code&gt; lists.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;result&lt;/code&gt; list also temporarily stores all &lt;code&gt;N&lt;/code&gt; characters before joining.&lt;/li&gt;
&lt;li&gt;Therefore, the space complexity is linear with respect to the length of the string &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Simulation Power:&lt;/strong&gt; Many string manipulation or pattern-based problems can be solved by directly simulating the process described. Break down the pattern into repeatable steps.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;State Management:&lt;/strong&gt; Keeping track of &lt;code&gt;current_row&lt;/code&gt; and &lt;code&gt;direction&lt;/code&gt; (or explicit phases like "down" and "up-right") is crucial for controlling the simulation.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Edge Cases First:&lt;/strong&gt; Always consider simple edge cases like &lt;code&gt;numRows = 1&lt;/code&gt; early on. They often simplify your logic or prevent errors.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;List of Lists for Structure:&lt;/strong&gt; When dealing with grid-like or row-based structures, a list of lists (or 2D array) is a very natural and effective data structure.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Concatenation Efficiency:&lt;/strong&gt; For building strings from many smaller pieces, appending to a list of characters/strings and then using &lt;code&gt;"".join(list)&lt;/code&gt; is generally more efficient in Python than repeated string concatenation (&lt;code&gt;+=&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope this breakdown helped you "unzigzag" this problem! Keep practicing, and you'll master these patterns in no time. Happy coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  Submission Details
&lt;/h2&gt;

&lt;p&gt;Author Account: aryan-subudhi&lt;br&gt;
Publishing Time: 2026-05-24 18:33:26&lt;br&gt;
Title: Unraveling the Zigzag: A Beginner's Guide to LeetCode 6 (Zigzag Conversion)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
