DEV Community

codechunker
codechunker

Posted on • Edited on

1 2

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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay