Allleetcode

#6 Zigzag Conversion LeetCode Java Problem Solved | Java Leetcode Solution

Zigzag Conversion LeetCode Java Problem Solved | Java Leetcode Solution

6. Zigzag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P     I    N
A   L S  I G
Y A   H R
P     I

Example 3:

Input: s = "A", numRows = 1
Output: "A"

Constraints:

  • 1 <= s.length <= 1000
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • 1 <= numRows <= 1000

Solution:

class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        
        int n = s.length();
        int sections = (int) Math.ceil(n / (2 * numRows - 2.0));
        int numCols = sections * (numRows - 1);
        
        char[][] matrix = new char[numRows][numCols];
        for (char[] row: matrix) {
            Arrays.fill(row, ' ');
        }
        
        int currRow = 0, currCol = 0;
        int currStringIndex = 0;
        
        // Iterate in zig-zag pattern on matrix and fill it with string characters.
        while (currStringIndex < n) {
            // Move down.
            while (currRow < numRows && currStringIndex < n) {
                matrix[currRow][currCol] = s.charAt(currStringIndex);
                currRow++;
                currStringIndex++;
            }
            
            currRow -= 2;
            currCol++;
            
            // Move up (with moving right also).
            while (currRow > 0 && currCol < numCols && currStringIndex < n) {
                matrix[currRow][currCol] = s.charAt(currStringIndex);
                currRow--;
                currCol++;
                currStringIndex++;
            }
        }
        
        StringBuilder answer = new StringBuilder();
        for (char[] row: matrix) {
            for (char character: row) {
                if (character != ' ') {
                    answer.append(character);
                }
            }
        }
        
        return answer.toString();
    }
}

Visit For More :- https://softwaretechit.com

HackerRank Problems Solution :- https://softwaretechit.com/category/hackerrank

Blog:- https://blog.softwaretechit.com

Shop:- https://shop.softwaretechit.com

Important Link’s :-https://home.softwaretechit.com

Projects :- https://link.softwaretechit.com

Quetions :-

How to print ZigZag in Java?

What is ZigZag conversion?

What is string ZigZag pattern?

What is example of zigzag?

What is zigzag formula?

What is the formula of high rate in zigzag code?

What is the value of zig zag?

What is zigzag array in Java?

What are the types of zigzag?

Why is it called Zig Zag?

Can a zigzag be a function?

Is zigzag a type of line?

What is Zig Zag used for?

What is the uses of zigzag?

What is the length of zigzag rule?

zigzag conversion leetcode solution

6. zigzag conversion java

zigzag conversion java

zigzag conversion – leetcode solution javascript

zig zag array leetcode

zigzag conversion geeksforgeeks

zigzag conversion python

zigzag conversion c#

leetcode,java,leetcode java,leetcode problems,leetcode solutions,problems,leetcode problems java,leetcode java solutions,leetcode 1,leetcode problem solving java,problem solving,two sum leetcode,leetcode easy problems,leetcode problems solve,leetcode solved problems,leetcode google problems,how to use leetcode effectively,solving leetcode problems,leetcode two sum,leetcode preparation,leetcode beginner problems,java leetcode #softwaretechit #2023 #tech

Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science