DEV Community

codechunker
codechunker

Posted on • Edited on

Solution to Leetcode's ZigZag Conversion: My thought process as a beginner

This is a medium Leetcode Problem. See below image for description:
problem description

ALGORITHM

1.Have a map of a row to a StringBuilder of the characters for that row;
2.Have a global counter (row): the current count of the row as you loop through;
3.boolean variable to know when to increment or decrement the row variable in step 2 above;
4.loop through the string, check if the current count of the global variable row is equal to the number of row; if it is, it should set the increment variable to false; else, it should set the increment variable to true;
5.increment/decrement row variable depending on the current value of the increment variable;
6.check the map for availability of the current row value. if it is not found, put the row with a new StringBuilder object;
7.Get the the value of the current row and append the current character to it;
8.loop through the map, append values to stringbuilder, then return string value of the stringbuilder.

OUTCOME
outcome image

LESSONS
After checking discussion section on Leetcode, I discovered that there was pattern to use. Learnt a lot from reading other people’s solutions.

Top comments (0)