-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigzagConversion.java
More file actions
139 lines (108 loc) · 3.54 KB
/
ZigzagConversion.java
File metadata and controls
139 lines (108 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package Algorithms.Strings;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 20 June 2025
* @link 6. Zigzag Conversion <a href="https://leetcode.com/problems/zigzag-conversion/">LeetCode link</a>
* @topics String, Array, Simulation
* Given s = "PAYPALISHIRING", numRows = 3
* Return "PAHNAPLSIIGYIR"
P A H N
A P L S I I G
Y I R
*
*/
public class ZigzagConversion {
public static void main(String[] args) {
String s = "PAYPALISHIRING"; // paypal is hiring
int numRows = 3;
System.out.println(convert(s, numRows));
}
/**
Given s = "PAYPALISHIRING", numRows = 3
P A H N
A P L S I I G
Y I R
n*m --> 2d array?
---------------
|P| |A| |H| |N|
---------------
|A|P|L|S|I|I|G|
--------------
|Y| |I| |R| | |
---------------
*/
public String convertMyApproach(String s, int numRows) {
int n = s.length(), m = numRows;
if(m==1) {
return s;
}
char[][] dp = new char[m][n];
int i=0;
int row = 0, col=0;
while(i<n) {
// down
for(;row<m && i<n; row++, i++) {
dp[row][col] = s.charAt(i);
}
row-=2;
col++;
// up-right diagonal
for(; row>=0 && i<n; row--, col++, i++) {
dp[row][col] = s.charAt(i);
}
col--;
row+=2;
}
StringBuilder sBuilder = new StringBuilder();
for(int r = 0; r<m; r++) {
for(int c=0; c<n; c++) {
if(dp[r][c] != 0) { // or dp[r][c] != '\u0000'
sBuilder.append(dp[r][c]);
}
}
}
return sBuilder.toString();
}
public static String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
StringBuilder res = new StringBuilder();
for (int row = 0; row < numRows; row++) { // each row
int increment = 2 * (numRows - 1); // The full cycle length
for (int i=row; i < s.length(); i += increment) {
res.append(s.charAt(i)); // Add the character at the current row
if (row > 0 && row < numRows-1 && (i+increment - 2 * row) < s.length()) {
// Add the character in the middle of the zigzag
res.append(s.charAt(i + increment - 2 * row));
}
}
}
return res.toString();
}
public static String convert2(String s, int numRows) {
if (numRows == 1 || numRows >= s.length()) {
return s; // No zigzag conversion needed
}
StringBuilder[] rows = new StringBuilder[numRows]; // or new StringBuilder[Math.min(numRows, s.length())]
for (int i = 0; i < rows.length; i++) {
rows[i] = new StringBuilder();
}
int currentRow = 0;
boolean goingDown = false;
for (char c : s.toCharArray()) {
rows[currentRow].append(c);
if (currentRow == 0) {
goingDown = true; // Start going down
} else if (currentRow == numRows - 1) {
goingDown = false; // Start going up
}
currentRow += goingDown ? 1 : -1; // Move to the next row
}
StringBuilder result = new StringBuilder();
for (StringBuilder row : rows) {
result.append(row);
}
return result.toString();
}
}