(String str,int start,int end)
| 8 | |
| 9 | } |
| 10 | public static long recursion(String str,int start,int end) |
| 11 | { |
| 12 | // base case |
| 13 | if(start>=str.length() || end<0) return 0; |
| 14 | // single element is always palindrome |
| 15 | if(start==end) return 1; |
| 16 | // check twice and subtract |
| 17 | else if (str.charAt(start) == str.charAt(end)) |
| 18 | return recursion(str,start+1, end) + recursion(str,start, end-1) + 1; |
| 19 | |
| 20 | else |
| 21 | return recursion(str,start+1, end) + recursion(str,start, end-1) - recursion(str,start+1, end-1); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | } |