Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

6. Zigzag Conversion

Medium49.9% Acceptance
String
Asked by:
A
Amazon
A
Adobe
ProblemSolutions (12)VideosCompanies (3)Notes

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"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

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"

Constraints:

  • 1 <= s.length <= 1000
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • 1 <= numRows <= 1000
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
P
PayPal

Approach

The key idea in Zigzag Conversion is to simulate how characters are placed when written in a zigzag pattern across multiple rows. Instead of actually drawing the zigzag grid, an efficient approach is to maintain a list of rows and append characters as you move down and up diagonally through the rows. A direction flag helps switch movement when the top or bottom row is reached.

For each character in the input string, add it to the current row and update the row index depending on the direction of traversal. After processing all characters, concatenate the rows to produce the final string. This approach avoids building a full 2D matrix and keeps memory usage minimal.

The algorithm processes each character once, leading to a time complexity of O(n), where n is the length of the string. The extra space required is also O(n) to store the row-wise results before combining them.

Complexity

ApproachTime ComplexitySpace Complexity
Row Simulation using Array of StringsO(n)O(n)
Cycle Pattern ObservationO(n)O(1) to O(n) depending on implementation

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Direct Zigzag Simulation

This approach involves simulating the zigzag pattern by using an array of strings to represent each row. We iterate through the string, placing each character in the appropriate row based on the current direction (down or up in the zigzag pattern). We change the direction whenever we hit the top or bottom row.

Time Complexity: O(n), where n represents the length of the input string, as we iterate through the string once.
Space Complexity: O(n), to store the zigzag rows.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5char* convert(char* s, int numRows

Explanation

The C solution implements a zigzag pattern simulation by creating an array of strings for each row. As we iterate through the input string, we add characters to the respective row. When we reach the top or bottom, we reverse direction. Finally, we concatenate all rows to produce the result.

Mathematical Index Calculation

This approach calculates the regular intervals for placing characters in the zigzag pattern without simulating the full grid. By deducing the mathematical relation of indices, characters are stored directly in the result string.

Time Complexity: O(n), where n is the input string length, owing to a complete pass through the characters.
Space Complexity: O(n), needed for the output string.

CC++JavaPythonC#JavaScript
1#

Video Solutions

Watch expert explanations and walkthroughs

ZigZag Conversion - Leetcode 6 - Python

NeetCode
12:46125,342 views

Asked By Companies

3 companies
A
Amazon
A
Adobe
P
PayPal

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Substring Without Repeating CharactersMedium
Longest Palindromic SubstringMedium
String to Integer (atoi)Medium
Regular Expression MatchingHard
More similar problems

Related Topics

String

Problem Stats

Acceptance Rate49.9%
DifficultyMedium
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Zigzag Conversion asked in FAANG interviews?

Yes, Zigzag Conversion is a well-known medium-level string problem often used in technical interviews. It tests pattern recognition, indexing logic, and efficient string manipulation.

What is the zigzag cycle length in this problem?

The zigzag pattern repeats in cycles of length 2 × (numRows − 1). Recognizing this pattern helps derive optimized solutions that calculate character positions mathematically instead of simulating movement.

What data structure is best for Zigzag Conversion?

A list or array of strings (or string builders) works best to represent each row of the zigzag pattern. This allows efficient appending as characters are processed sequentially.

What is the optimal approach for Zigzag Conversion?

The optimal approach simulates the zigzag traversal using an array of rows. You append characters while moving down and then diagonally up between rows. After processing the string, concatenating all rows produces the final result in O(n) time.

Previous Problem

Longest Palindromic Substring

Next Problem

String to Integer (atoi)

)
{
6
if
(
numRows
==
1
)
return
s
;
7
int
len
=
strlen
(
s
)
;
8
char
*
*
rows
=
(
char
*
*
)
malloc
(
sizeof
(
char
*
)
*
numRows
)
;
9
for
(
int
i
=
0
;
i
<
numRows
;
i
++
)
{
10
rows
[
i
]
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
len
)
;
11
rows
[
i
]
[
0
]
=
'\0'
;
12
}
13
int
currentRow
=
0
,
step
=
1
;
14
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
15
strncat
(
rows
[
currentRow
]
,
&
s
[
i
]
,
1
)
;
16
if
(
currentRow
==
0
)
step
=
1
;
17
else
if
(
currentRow
==
numRows
-
1
)
step
=
-
1
;
18
currentRow
+=
step
;
19
}
20
char
*
result
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
len
+
1
)
)
;
21
result
[
0
]
=
'\0'
;
22
for
(
int
i
=
0
;
i
<
numRows
;
i
++
)
{
23
strcat
(
result
,
rows
[
i
]
)
;
24
free
(
rows
[
i
]
)
;
25
}
26
free
(
rows
)
;
27
return
result
;
28
}
include
<stdio.h>
2
#
include
<string.h>
3
4
char
*
convert
(
char
*
s
,
int
numRows
)
{
5
if
(
numRows
==
1
)
return
s
;
6
int
len
=
strlen
(
s
)
;
7
char
*
result
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
len
+
1
)
)
;
8
int
pos
=
0
;
9
int
cycleLen
=
2
*
numRows
-
2
;
10
for
(
int
i
=
0
;
i
<
numRows
;
i
++
)
{
11
for
(
int
j
=
0
;
j
+
i
<
len
;
j
+=
cycleLen
)
{
12
result
[
pos
++
]
=
s
[
j
+
i
]
;
13
if
(
i
!=
0
&&
i
!=
numRows
-
1
&&
j
+
cycleLen
-
i
<
len
)
14
result
[
pos
++
]
=
s
[
j
+
cycleLen
-
i
]
;
15
}
16
}
17
result
[
pos
]
=
'\0'
;
18
return
result
;
19
}

Explanation

This C solution calculates the correct indices for zigzag order dynamically. By understanding the repetitive cycle length, it extracts characters directly in order. The outer loop runs over each row index, and within it, character indices are calculated for the zigzag transformation.