Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Answer implements PalindromicPathsInterface {
- String[] strings;
- int count =0;
- public String[] findPalindromicPaths(String[] input)
- {
- if(input.length==0)
- {
- String[] strings=new String[1];
- strings[0]="NULL";
- return null;
- }
- func(input,0,0,"");
- strings=new String[count];
- count=0;
- func1(input,0,0,"");
- return strings;
- }
- public void func(String[] A, int i, int j, String curr)
- {
- if(i == A.length - 1 && j == A[0].length() - 1){
- curr = curr + A[i].charAt(j);
- String temp = new StringBuffer(curr).reverse().toString();
- if(temp.equals(curr))
- {
- count++;
- }
- }
- else{
- if(i + 1 < A.length)
- func(A, i+1, j, curr + A[i].charAt(j));
- if(j + 1 < A[0].length())
- func(A, i, j+1, curr + A[i].charAt(j));
- if(i == 0 && j == 0)
- {
- return ;
- }
- }
- }
- public void func1(String[] A, int i, int j, String curr)
- {
- if(i == A.length - 1 && j == A[0].length() - 1){
- curr = curr + A[i].charAt(j);
- String temp = new StringBuffer(curr).reverse().toString();
- if(temp.equals(curr))
- {
- strings[count++]=curr;
- }
- }
- else{
- if(i + 1 < A.length)
- func1(A, i+1, j, curr + A[i].charAt(j));
- if(j + 1 < A[0].length())
- func1(A, i, j+1, curr + A[i].charAt(j));
- if(i == 0 && j == 0)
- {
- return ;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment