Skip to content

Commit 77d007c

Browse files
Add Zigzag Conversion
1 parent d7addae commit 77d007c

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This repository contains my solutions to various LeetCode problems. Each solutio
66

77
## LeetCode Stats
88
- **Profile**: [Vinnn_96](https://leetcode.com/u/Vinnn_96/)
9-
- **Problems Solved**: 7
9+
- **Problems Solved**: 8
1010
- **Languages**: Java
1111

1212
## Repository Structure
@@ -24,14 +24,15 @@ Each problem has its own directory containing:
2424
| 3 | [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters) | Medium | [Java](Longest%20Substring%20Without%20Repeating%20Characters/Solution.java) | Hash Table, String, Sliding Window |
2525
| 4 | [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays) | Hard | [Java](Median%20of%20Two%20Sorted%20Arrays/Solution.java) | Array, Binary Search, Divide and Conquer |
2626
| 5 | [Longest Palindromic Substring](Longest%20Palindromic%20Substring) | Medium | [Java](Longest%20Palindromic%20Substring/Solution.java) | Two Pointers, String, Dynamic Programming |
27+
| 6 | [Zigzag Conversion](Zigzag%20Conversion) | Medium | [Java](Zigzag%20Conversion/Solution.java) | String |
2728
| 214 | [Shortest Palindrome](Shortest%20Palindrome) | Hard | [Java](Shortest%20Palindrome/Solution.java) | String, Rolling Hash, String Matching, Hash Function |
2829
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
2930

3031
## Categories
3132

3233
### By Difficulty
3334
- Easy: 1
34-
- Medium: 4
35+
- Medium: 5
3536
- Hard: 2
3637

3738
### By Topics
@@ -44,6 +45,7 @@ Each problem has its own directory containing:
4445
- String Problems
4546
- [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters)
4647
- [Longest Palindromic Substring](Longest%20Palindromic%20Substring)
48+
- [Zigzag Conversion](Zigzag%20Conversion)
4749
- [Shortest Palindrome](Shortest%20Palindrome)
4850
- Dynamic Programming Problems
4951
- [Longest Palindromic Substring](Longest%20Palindromic%20Substring)

Zigzag Conversion/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Zigzag Conversion
2+
3+
## LeetCode Problem
4+
- Problem Number: 6
5+
- Problem Link: https://leetcode.com/problems/zigzag-conversion/
6+
- Difficulty: Medium
7+
- Topics: String
8+
9+
## Problem Description
10+
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
11+
12+
P A H N
13+
A P L S I I G
14+
Y I R
15+
16+
And then read line by line: "PAHNAPLSIIGYIR"
17+
18+
Write the code that will take a string and make this conversion given a number of rows:
19+
20+
string convert(string s, int numRows);
21+
22+
## Examples
23+
24+
### Example 1:
25+
```
26+
Input: s = "PAYPALISHIRING", numRows = 3
27+
Output: "PAHNAPLSIIGYIR"
28+
```
29+
30+
### Example 2:
31+
```
32+
Input: s = "PAYPALISHIRING", numRows = 4
33+
Output: "PINALSIGYAHRPI"
34+
Explanation:
35+
P I N
36+
A L S I G
37+
Y A H R
38+
P I
39+
```
40+
41+
### Example 3:
42+
```
43+
Input: s = "A", numRows = 1
44+
Output: "A"
45+
```
46+
47+
## Constraints
48+
- 1 <= s.length <= 1000
49+
- s consists of English letters (lower-case and upper-case), digits, and spaces.
50+
- 1 <= numRows <= 1000

Zigzag Conversion/Solution.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public String convert(String s, int numRows) {
3+
if (numRows == 1 || s.length() <= numRows) {
4+
return s;
5+
}
6+
7+
StringBuilder[] rows = new StringBuilder[numRows];
8+
for (int i = 0; i < numRows; i++) {
9+
rows[i] = new StringBuilder();
10+
}
11+
12+
int currentRow = 0;
13+
boolean goingDown = false;
14+
15+
for (char c : s.toCharArray()) {
16+
rows[currentRow].append(c);
17+
if (currentRow == 0 || currentRow == numRows - 1) {
18+
goingDown = !goingDown;
19+
}
20+
currentRow += goingDown ? 1 : -1;
21+
}
22+
23+
StringBuilder result = new StringBuilder();
24+
for (StringBuilder row : rows) {
25+
result.append(row);
26+
}
27+
28+
return result.toString();
29+
}
30+
}

0 commit comments

Comments
 (0)