# Leetcode Zigzag Conversion problem solution in C#

Feb 13, 2023

In the Leetcode Zigzag Conversion problem solution in C# programming 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);

## Leetcode Zigzag Conversion problem solution in C# programming

``````public class Solution {
public string Convert(string s, int numRows) {
if (s.Length <= 2 || numRows == 1)
return s;

StringBuilder sb = new StringBuilder();
int space=numRows*2-2;

for (int i=numRows; i>=1; i--){
int gap= i*2-2, start=numRows-i;

while (start < s.Length){
sb.Append(s[start]); // down
if((gap != space && gap != 0)&& (start+gap)<s.Length)
sb.Append(s[start+gap]); //up

start= start+ space;
}
}

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

#### By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.