Given a matrix containing lower alphabetical characters only, we need to count number of palindromic paths in given matrix. A path is defined as a sequence of cells starting from **top-left cell** and ending at **bottom-right **cell. We are allowed to move to **right and down only** from current cell.

Examples:

Input : mat[][] = {"aaab”, "baaa” “abba”} Output : 3 Number of palindromic paths are 3 from top-left to bottom-right. aaaaaa (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) -> (1, 3) -> (2, 3) aaaaaa (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (1, 3) -> (2, 3) abaaba (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) -> (2, 2) -> (2, 3)

We can solve this problem recursively, we start from two corners of a palindromic path(top-left and bottom right). In each recursive call, we maintain a state which will constitute two cells one from starting and one from end which should be equal for palindrome property. If at a state, both cell characters are equal then we call recursively with all possible movements in both directions.

As this can lead to solving same subproblem multiple times, we have taken a map memo in below code which stores the calculated result with key as indices of starting and ending cell so if subproblem with same starting and ending cell is called again, result will be returned by memo directly instead of recalculating again.

Please see below code for better understanding,

`// C++ program to get number of palindrome ` `// paths in matrix ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `#define R 3 ` `#define C 4 ` ` ` `// struct to represent state of recursion ` `// and key of map ` `struct` `cells ` `{ ` ` ` `// indices of front cell ` ` ` `int` `rs, cs; ` ` ` ` ` `// indices of end cell ` ` ` `int` `re, ce; ` ` ` `cells(` `int` `rs, ` `int` `cs, ` `int` `re, ` `int` `ce) : ` ` ` `rs(rs), cs(cs), re(re), ce(ce) { } ` ` ` ` ` `// operator overloading to compare two ` ` ` `// cells which rs needed for map ` ` ` `bool` `operator <(` `const` `cells& other) ` `const` ` ` `{ ` ` ` `return` `((rs != other.rs) || (cs != other.cs) || ` ` ` `(re != other.re) || (ce != other.ce)); ` ` ` `} ` `}; ` ` ` `// recursive method to return number of palindromic ` `// paths in matrix ` `// (rs, cs) ==> Indices of current cell from a starting ` `// point (First Row) ` `// (re, ce) ==> Indices of current cell from a ending ` `// point (Last Row) ` `// memo ==> To store results of already computed ` `// problems ` `int` `getPalindromicPathsRecur(` `char` `mat[R][C], ` `int` `rs, ` ` ` `int` `cs, ` `int` `re, ` `int` `ce, map<cells, ` `int` `>& memo) ` `{ ` ` ` `// Base Case 1 : if any index rs out of boundary, ` ` ` `// return 0 ` ` ` `if` `(rs < 0 || rs >= R || cs < 0 || cs >= C) ` ` ` `return` `0; ` ` ` `if` `(re < 0 || re < rs || ce < 0 || ce < cs) ` ` ` `return` `0; ` ` ` ` ` `// Base case 2 : if values are not equal ` ` ` `// then palindrome property rs not satisfied, ` ` ` `// so return 0 ` ` ` `if` `(mat[rs][cs] != mat[re][ce]) ` ` ` `return` `0; ` ` ` ` ` `// If we reach here, then matrix cells are same. ` ` ` ` ` `// Base Case 3 : if indices are adjacent then ` ` ` `// return 1 ` ` ` `if` `(` `abs` `((rs - re) + (cs - ce)) <= 1) ` ` ` `return` `1; ` ` ` ` ` `// if result rs precalculated, return from map ` ` ` `if` `(memo.find(cells(rs, cs, re, ce)) != memo.end()) ` ` ` `return` `memo[cells(rs, cs, re, ce)]; ` ` ` ` ` `int` `ret = 0; ` `// Initialize result ` ` ` ` ` `// calling recursively for all possible movements ` ` ` `ret += getPalindromicPathsRecur(mat, rs + 1, cs, ` ` ` `re - 1, ce, memo); ` ` ` `ret += getPalindromicPathsRecur(mat, rs + 1, cs, re, ` ` ` `ce - 1, memo); ` ` ` `ret += getPalindromicPathsRecur(mat, rs, cs + 1, ` ` ` `re - 1, ce, memo); ` ` ` `ret += getPalindromicPathsRecur(mat, rs, cs + 1, re, ` ` ` `ce - 1, memo); ` ` ` ` ` `// storing the calculated result in map ` ` ` `memo[cells(rs, cs, re, ce)] = ret; ` ` ` ` ` `return` `ret; ` `} ` ` ` `// method returns number of palindromic paths in matrix ` `int` `getPalindromicPaths(` `char` `mat[R][C]) ` `{ ` ` ` `map<cells, ` `int` `> memo; ` ` ` `return` `getPalindromicPathsRecur(mat, 0, 0, R - 1, ` ` ` `C - 1, memo); ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `char` `mat[R][C] = ` ` ` `{ ` ` ` `'a'` `, ` `'a'` `, ` `'a'` `, ` `'b'` `, ` ` ` `'b'` `, ` `'a'` `, ` `'a'` `, ` `'a'` `, ` ` ` `'a'` `, ` `'b'` `, ` `'b'` `, ` `'a'` ` ` `}; ` ` ` `printf` `(` `"%d"` `, getPalindromicPaths(mat)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3

Time Complexity : O(R x C)

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Print all palindromic paths from top left to bottom right in a matrix
- Count of palindromic plus paths in a given Matrix
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Maths behind number of paths in matrix problem
- Total number of decreasing paths in a matrix
- Count unique paths is a matrix whose product of elements contains odd number of divisors
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Paths from entry to exit in matrix and maximum path sum
- Sum of cost of all paths to reach a given cell in a Matrix
- Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Count of odd length contiguous Palindromic sequences in a Matrix