Zigzag String Time : Space : 123456789101112131415161718string Solution::convert(string A, int B) { if(B == 1) return A; int left = (B - 1) * 2, right = 0; string res = ""; for(int i = 0; i < B; i++,left -= 2, right += 2) { bool fl = true; int j = i; while(j < A.length()) { res.push_back(A[j]); if(left == 0 or right == 0) j += (B - 1) * 2; else if(fl) j += left; else j += right; fl = !fl; } } return res;}