Skip to content

Commit ae51b5a

Browse files
committed
fixes
1 parent ca17534 commit ae51b5a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Dynamic Programming/ZigZag Conversion_leetcode.java renamed to Dynamic Programming/zigzag_conversion.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
public class MyClass {
4141
public static String convert(String s, int numRows) {
4242
int flag = -1;
43+
4344
HashMap < Integer, String > map = new HashMap < > ();
4445
int pos = 0;
4546
String word = "";
4647
if (numRows <= 1)
4748
return s;
4849
else {
50+
// traversing in zig zag pattern and storing the data of each row in hashtable as string
4951
for (int x = 0; x < s.length(); x++) {
5052
char ch1 = s.charAt(x);
5153
String ch = Character.toString(ch1);
@@ -54,27 +56,31 @@ public static String convert(String s, int numRows) {
5456
flag = 1;
5557
if (map.containsKey(pos)) {
5658
String a = map.get(pos);
59+
// concatinating the previous word along with new character
5760
String w = a + ch;
5861
map.put(pos, w);
5962
} else {
6063
map.put(pos, ch);
6164
}
65+
// once reached the end of the row start moving upwards i.e decreasing the key in hash table
6266
pos--;
63-
64-
} else if (pos == 0 || flag == 0) {
67+
}
68+
else if (pos == 0 || flag == 0) {
6569
flag = 0;
70+
// check if the key in hash table has some data present
6671
if (map.containsKey(pos)) {
6772
String a = map.get(pos);
6873
String w = a + ch;
69-
7074
map.put(pos, w);
7175
} else {
7276
map.put(pos, ch);
7377
}
78+
// keep increasing the key i.e pos in hash table till we reach at the end of the row
7479
pos++;
7580
}
7681
}
7782
for (Map.Entry < Integer, String > e: map.entrySet())
83+
// concatinate the values of each row of hash table
7884
word = word + e.getValue();
7985

8086
return word;

0 commit comments

Comments
 (0)