package com.fishercoder.solutions;

/**
 * 6. ZigZag Conversion
 *
 * 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 text, int nRows);
 convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
 */

public class _6 {

	public String convert(String s, int numRows) {
		StringBuilder[] sb = new StringBuilder[numRows];
		char[] c = s.toCharArray();
		int len = s.length();
		for (int i = 0; i < numRows; i++) {
			sb[i] = new StringBuilder();//this is an important step to initialize it
		}
		int i = 0;
		while (i < len) {
			for (int index = 0; index < numRows && i < len; index++) {
				sb[index].append(c[i++]);// vertically down
			}

			for (int index = numRows - 2; index >= 1 && i < len; index--) {
				/**Why it should start from numRows - 2? Think of the example when numRows = 3
				 the starting point of obliquely going up is 1, which is numRows-2.*/
				sb[index].append(c[i++]);// obliquely up
			}
		}

		for (i = 1; i < numRows; i++) {
			sb[0].append(sb[i]);
		}
		return sb[0].toString();
	}

}

@@ -544,17 +544,50 @@
 ss _6 %7B%0A
-%0A
+%09public static class Solution1 %7B%0A%09
 %09public 
@@ -628,16 +628,17 @@
 ws) %7B%0A%09%09
+%09
 StringBu
@@ -678,16 +678,17 @@
 mRows%5D;%0A
+%09
 %09%09char%5B%5D
@@ -711,16 +711,17 @@
 ay();%0A%09%09
+%09
 int len 
@@ -734,16 +734,17 @@
 ngth();%0A
+%09
 %09%09for (i
@@ -776,16 +776,17 @@
 +) %7B%0A%09%09%09
+%09
 sb%5Bi%5D = 
@@ -852,18 +852,20 @@
 ze it%0A%09%09
+%09
 %7D%0A
+%09
 %09%09int i 
@@ -869,16 +869,17 @@
  i = 0;%0A
+%09
 %09%09while 
@@ -889,24 +889,25 @@
 %3C len) %7B%0A%09%09%09
+%09
 for (int ind
@@ -953,24 +953,25 @@
 ex++) %7B%0A%09%09%09%09
+%09
 sb%5Bindex%5D.ap
@@ -1002,16 +1002,17 @@
 ly down%0A
+%09
 %09%09%09%7D%0A%0A%09%09
@@ -1008,24 +1008,25 @@
 n%0A%09%09%09%09%7D%0A%0A%09%09%09
+%09
 for (int ind
@@ -1073,24 +1073,25 @@
  index--) %7B%0A
+%09
 %09%09%09%09/**Why i
@@ -1165,16 +1165,17 @@
 = 3%0A%09%09%09%09
+%09
  the sta
@@ -1236,16 +1236,17 @@
 ws-2.*/%0A
+%09
 %09%09%09%09sb%5Bi
@@ -1289,15 +1289,18 @@
 %0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A%0A
+%09
 %09%09fo
@@ -1332,16 +1332,17 @@
 +) %7B%0A%09%09%09
+%09
 sb%5B0%5D.ap
@@ -1354,22 +1354,24 @@
 sb%5Bi%5D);%0A
+%09
 %09%09%7D%0A%09%09
+%09
 return s
@@ -1387,14 +1387,18 @@
 ring();%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

refactor 6