-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlantedCipherTextSpecialDiagonalTraverse.java
More file actions
28 lines (28 loc) · 1.09 KB
/
SlantedCipherTextSpecialDiagonalTraverse.java
File metadata and controls
28 lines (28 loc) · 1.09 KB
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
class Solution {
// phle encoded text ko matrix ke andr put kro and diagonal traverse krke word ko print krao
// diagonal traverse ke liye top left to bottom right jana hai toh row toh saari traverse honi hai baari baari loop ke andr column ko increase krte jao required jb tk na ho puri
public String decodeCiphertext(String encodedText, int rows) {
int cols=encodedText.length()/rows;
int index=0;
char[][] matrix=new char[rows][cols];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
matrix[i][j]=encodedText.charAt(index++);
}
}
StringBuilder sb=new StringBuilder();
for(int col=0;col<cols;col++){
int row=0;
int currentColumn=col;
while(row<rows && currentColumn<cols){
sb.append(matrix[row][currentColumn]);
row++;
currentColumn++;
}
}
while(sb.length()>0 && sb.charAt(sb.length()-1)==' '){
sb.deleteCharAt(sb.length()-1);
}
return sb.toString();
}
}