# How come convert("ABC", 2) is ACB not ABC?

• How come convert("ABC", 2) is ACB not ABC?

• It's zigzagged as:

A C
B

Thus it's ACB

• I think maybe you make the same mistake as me. At first, when i see the example with 3 rows, i think the 4th letter is in the middle row, but it's not. Actualy, the letters go like a snake, it go from top row to bottom row and then go up to the top row. Maybe it will be better to have an example of more than 3 rows, especially for people from non-English contry like me who doesn't really understand the word "zigzag"...

• @hefoxed @zhaoyi.yuan31 Thanks, both of you guys. Got accepted!

``````public String convert(String str, int R) {
if (R == 1) return str;

char[] s = str.toCharArray();
char[] t = new char[s.length];

for (int i = 0, len = 0; i < R; i++) {
for (int j = 0, k = i; k < s.length; j++) {
t[len++] = s[k];
k += ((i == 0 || j % 2 == 0) && i != R - 1 ? 2 * (R - i - 1) : 2 * i);
}
}

return new String(t);
}``````

• @zhaoyi.yuan31 it would be gr8 if you explain for the testcase ("PAYPALISHIRING" ,4)

• anyone please explain some more testcases

• @aman take an accepted solution (one is available in this thread) and run for any input you want.

• @snap_dragon thanks but i can get the output from expected output also, i am asking for explanation as i am not getting what actually the objective is

• @zhaoyi.yuan31 Thanks for the explanation! I got so confused by this question itself

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.