Skip to content

Commit 783c9c8

Browse files
authored
solution(cpp,java): 6. Zigzag Conversion
6. Zigzag Conversion - C++ - Java
2 parents 768adac + a8dc4d4 commit 783c9c8

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

Medium/6. Zigzag Conversion/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ZigZag Conversion 🔄
2+
3+
The ZigZag Conversion problem asks you to convert a given string into a new string by reordering the characters in a specific pattern determined by the number of rows.
4+
5+
## Approach 🔍
6+
7+
### Input Validation 📌
8+
9+
- If the input string is empty, has only one character, or the number of rows is 1, return the input string as it is, as there's no need for conversion in these cases.
10+
11+
### Initialization 🚀
12+
13+
- Initialize an empty string `ans` to store the converted result.
14+
- Get the length `n` of the input string.
15+
- Initialize an index variable `ind` to 0 and a row counter `r` to 0.
16+
17+
### Conversion Logic ⚙️
18+
19+
- Iterate through each row, from row 0 to `numRows - 1`.
20+
- In each row, iterate through the input string, adding characters to the result string `ans`.
21+
22+
### Character Appending ✏️
23+
24+
- Append the current character to `ans`.
25+
- If we are not in the first row, last row, and there's a valid next character within the string bounds:
26+
- Append the character at an offset of `2 * i` from the current position, where `i` is the current row.
27+
- Calculate the next index position based on the row and whether it's the first row or not:
28+
- If it's the first row or the last row, move by `2 * (numRows - 1)` steps.
29+
- Otherwise, move by `2 * (numRows - i - 1)` steps.
30+
31+
### Resetting for the Next Row ♻️
32+
33+
- After finishing one row, reset `ind` to the next row's starting position (which is `i + 1`).
34+
- Reset `r` to 0 for the new row.
35+
36+
### Return the Result ✅
37+
38+
- Return the final result string `ans`.
39+
40+
This approach follows the ZigZag pattern and efficiently constructs the converted string.
41+
42+
Now you can implement this approach in code to solve the ZigZag Conversion problem.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
string convert(string s, int numRows) {
4+
string ans = "";
5+
int n = s.length();
6+
int ind = 0, r = 0;
7+
// If the string is empty, has only one character, or numRows is 1, return the input string as-is.
8+
if (n <= 2 || numRows == 1)
9+
return s;
10+
11+
for (int i = 0; i < numRows; i++) {
12+
while (ind < n) {
13+
// Append the current character to the answer string.
14+
ans += s[ind];
15+
16+
// If we are not in the first row, last row, and the next character within the string bounds,
17+
// append the character at an offset of 2*i from the current position.
18+
if (r > 0 && i > 0 && i < numRows - 1 && ind + 2 * i <= n - 1) {
19+
ans += s[ind + 2 * i];
20+
}
21+
22+
// Calculate the next index position based on the row and whether it's the first row or not.
23+
if ((r == 0 || i == 0) && i != numRows - 1)
24+
ind += 2 * (numRows - i - 1);
25+
else
26+
ind += 2 * (numRows - 1);
27+
28+
r++;
29+
}
30+
// Reset ind to the next row's starting position and reset r to 0 for the new row.
31+
ind = i + 1;
32+
r = 0;
33+
}
34+
return ans;
35+
}
36+
};
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public String convert(String s, int numRows) {
3+
if(numRows < 2) {
4+
return s;
5+
}
6+
String str ="";
7+
int x = s.length();
8+
9+
char ans[][] = new char[numRows][x];
10+
for(int i = 0 ; i < numRows; i++) {
11+
for(int j = 0; j < x; j++) {
12+
ans[i][j] = '&';
13+
}
14+
}
15+
for(int i = 0, changer = 1, row = 0; i < s.length(); i++) {
16+
ans[row][i] = s.charAt(i);
17+
if(row == numRows - 1) {
18+
changer = -1;
19+
}
20+
if(row == 0) {
21+
changer = 1;
22+
}
23+
row = row + changer;
24+
}
25+
for(int i = 0 ; i < numRows; i++) {
26+
for(int j = 0; j < x; j++) {
27+
if(ans[i][j] != '&') {
28+
str = str + ans[i][j];
29+
}
30+
}
31+
}
32+
33+
return str;
34+
}
35+
}

Medium/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| **No.** | **Problem** |
44
| :------: | :---------------------------------------------------------------------------------------------------------------------- |
55
| **2** | [Add Two Numbers](2.%20Add%20Two%20Numbers/) |
6+
| **6** | [Zigzag Conversion](6.%20Zigzag%20Conversion/) |
67
| **7** | [Reverse Integer](7.%20Reverse%20Integer/) |
78
| **8** | [String to Integer (atoi)](<8.%20String%20to%20Integer%20(atoi)/>) |
89
| **11** | [Container With Most Water](11.%20Container%20With%20Most%20Water/) |

0 commit comments

Comments
 (0)