Skip to content

Commit 52c416e

Browse files
authored
Merge pull request #558 from Ananya-Misra/leetcode
Add Zig Zag Conversion
2 parents 13aa69b + f2f889d commit 52c416e

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// PROBLEM STATEMENT ALONG WITH TEST CASE
2+
// 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)
3+
4+
// P A H N
5+
// A P L S I I G
6+
// Y I R
7+
// And then read line by line: "PAHNAPLSIIGYIR"
8+
9+
// Write the code that will take a string and make this conversion given a number of rows:
10+
11+
// string convert(string s, int numRows);
12+
13+
14+
// Example 1:
15+
16+
// Input: s = "PAYPALISHIRING", numRows = 3
17+
// Output: "PAHNAPLSIIGYIR"
18+
// Example 2:
19+
20+
// Input: s = "PAYPALISHIRING", numRows = 4
21+
// Output: "PINALSIGYAHRPI"
22+
// Explanation:
23+
// P I N
24+
// A L S I G
25+
// Y A H R
26+
// P I
27+
// Example 3:
28+
29+
// Input: s = "A", numRows = 1
30+
// Output: "A"
31+
32+
33+
// Constraints:
34+
35+
// 1 <= s.length <= 1000
36+
// s consists of English letters (lower-case and upper-case), ',' and '.'.
37+
// 1 <= numRows <= 1000
38+
39+
import java.util.*;
40+
public class MyClass {
41+
public static String convert(String s, int numRows) {
42+
int flag = -1;
43+
44+
HashMap < Integer, String > map = new HashMap < > ();
45+
int pos = 0;
46+
String word = "";
47+
if (numRows <= 1)
48+
return s;
49+
else {
50+
// traversing in zig zag pattern and storing the data of each row in hash map as string
51+
for (int x = 0; x < s.length(); x++) {
52+
char ch1 = s.charAt(x);
53+
String ch = Character.toString(ch1);
54+
55+
if (pos == (numRows - 1) || flag == 1 && pos > 0) {
56+
flag = 1;
57+
if (map.containsKey(pos)) {
58+
String a = map.get(pos);
59+
// concatinating the previous word along with new character
60+
String w = a + ch;
61+
map.put(pos, w);
62+
} else {
63+
map.put(pos, ch);
64+
}
65+
// once reached the end of the row start moving upwards i.e decreasing the key in hash map
66+
pos--;
67+
}
68+
else if (pos == 0 || flag == 0) {
69+
flag = 0;
70+
// check if the key in hash map has some data present
71+
if (map.containsKey(pos)) {
72+
String a = map.get(pos);
73+
String w = a + ch;
74+
map.put(pos, w);
75+
} else {
76+
map.put(pos, ch);
77+
}
78+
// keep increasing the key i.e pos in hash map till we reach at the end of the row
79+
pos++;
80+
}
81+
}
82+
for (Map.Entry < Integer, String > e: map.entrySet())
83+
// concatinate the values of each row of hash map
84+
word = word + e.getValue();
85+
86+
return word;
87+
}
88+
}
89+
//main function
90+
public static void main(String args[]) {
91+
Scanner sc = new Scanner(System.in);
92+
String s = sc.next();
93+
int rows = sc.nextInt();
94+
System.out.println(convert(s, rows));
95+
96+
}
97+
}

0 commit comments

Comments
 (0)