You are currently viewing #6 Zigzag Conversion LeetCode Java Problem Solved | Java Leetcode Solution

#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