# I don't know why my code did not pass a specific test case

• Hi All,

Here is my code, I don't why it fails in this test case:

Input: "ABC",2

Output: "ACB"

Expected: "ACB"

class Solution {
public:
string convert(string s, int numRows) {

``````    int len=s.length();
if ((!len)|| (len<=numRows) || numRows <= 1) return s;

int i, j;
string sstr = "";
vector<string> res(numRows);

for (i = 0; i<s.length(); i += (2 * numRows - 2))
{
for (j = 0; j<(2 * numRows - 2); j++)
{
if (j<numRows)
{
res[j] += s[i + j];
}
else
{
res[j - numRows + 1] += s[i + j];
}
}
}

for (i = 0; i<numRows; i++)
{
sstr += res[i];
}
return sstr;
}
``````

};

• else
{
res[j - numRows + 1] += s[i + j];
}

should be

else
{
res[2numRows - j*] += s[i + j];
}

You may also want to add this check (i + j) < len

for (j = 0; (j<(2 * numRows - 2) && (i + j < len)); j++)

but
else { res[2numRows - j] += s[i + j]; }
should be else { res[2
numRows -2- j] += s[i + j]; }

• How is ACB the expected answer? The grid would look like this
A
BC

So output should be ABC isnt it?

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