-
Notifications
You must be signed in to change notification settings - Fork 31
/
ZigZag.java
43 lines (37 loc) · 995 Bytes
/
ZigZag.java
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
package String;
/**
* Author - archit.s
* Date - 09/10/18
* Time - 12:40 PM
*/
public class ZigZag {
private String convert(String A, int B) {
if(A.length() == 1 || B == 1){
return A;
}
StringBuilder s = new StringBuilder();
int i =0;
while(s.length()<A.length()){
int j = i;
boolean flip = false;
while(j<A.length() && s.length()<A.length()){
s.append(A.charAt(j) );
if(!flip){
if(i == B - 1){
i = 0;
}
j = j + 2*(B-i-1);
}
else{
j = j + 2*(i);
}
flip = (i == 0 || i == B-1) ? false : !flip;
}
i++;
}
return s.toString();
}
public static void main(String[] args) {
System.out.println(new ZigZag().convert("PAYPALISHIRING", 5));
}
}