# Zigzag Pattern String Conversion - Leet Code Solution

August 27, 2019

## 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:

``````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``````

## Solution

If you look the pattern, its going in two directions:

• Top to bottom straight
• Bottom to top with one incremented index

You need one flag to indicate whether you are going downward or going upward.

## Algorithm

• Maintain a flag to indicate whether you are going upward or downward. Reset boundaries are on 0-index and length-1 index.
• Toggle the flag on reaching boundaries (top most, or bottom most)
• Maintain a List of string(StringBuffer in Java). List length is equal to the number of rows asked.
• Maintain a variable which will tell you in which stringbuffer(row num) to add the current character

## Code

``````public class Q6_ZigZagConversion {
private String str;

public Q6_ZigZagConversion(String str) {
this.str = str;
}

public String conversion(int numRows) {
int l = this.str.length();
List<StringBuffer> zigzag = new ArrayList<StringBuffer>();
for (int i=0; i<numRows; i++) {
}

boolean comingFromTop = true;
int zig = 0;
for (int i=0; i<l; i++) {
zigzag.get(zig).append(this.str.charAt(i));

if (zig == numRows-1) {
comingFromTop = false;
}
else if (zig == 0) {
comingFromTop = true;
}
zig = comingFromTop ? zig + 1 : zig - 1;
zig = zig % numRows;
}

StringBuffer sb = new StringBuffer();
for (int i=0; i<numRows; i++) {
sb.append(zigzag.get(i));
}

return sb.toString();
}
}``````

## Similar Posts

### Four Sum - Leet Code Solution

Problem Statement Given an array nums of n integers and an integer target, are…

### How to calculate First Common Ancestor of two Nodes in Binary Tree

First try to understand question. Its a binary tree, not a binary search tree…

### Remove Duplicates from Sorted Array - Leet Code Solution

Problem Statement Given a sorted array nums, remove the duplicates in-place such…

### Graph Topological Sorting - Build System Order Example

Graph Topological Sorting This is a well known problem in graph world…

### Counting Inversions Coding Problem

** Inversion There is an array(a) and two indexes i and j. Inversion is the…

### Rotate Image - Leet Code Solution

Problem Statement You are given an n x n 2D matrix representing an image, rotate…

## Latest Posts

### Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

### Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

### Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Introduction In one of my app, I was using to talk to . I have used some event…

### Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

Introduction So you have a Django project, and want to run it using docker image…

### Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

### Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…