Palindrome pair in an array of words ( or strings ) | C # program to check if there is a pair that of above method using Trie ; Alphabet size ( # of symbols ) ; Trie node ; public List < int > pos ; To store palindromic positions in str ; isLeaf is true if the node represents end of a word ; constructor ; Utility function to check if a string is a palindrome ; compare each character from starting with its corresponding character from last ; If not present , inserts reverse of key into Trie . If the key is prefix of a Trie node , just mark leaf node ; Start traversing word from the last ; If it is not available in Trie , then store it ; If current word is palindrome till this level , store index of current word . ; mark last node as leaf ; list to store result ; Returns true if key presents in Trie , else false ; If it is present also check upto which index it is palindrome ; If not present then return ; Function to check if a palindrome pair exists ; Construct trie ; Search for different keys ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int ALPHABET_SIZE = 26 ; class TrieNode { public TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; public int id ; public Boolean isLeaf ; public TrieNode ( ) { isLeaf = false ; pos = new List < int > ( ) ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) children [ i ] = null ; } } static Boolean isPalindrome ( String str , int i , int len ) { while ( i < len ) { if ( str [ i ] != str [ len ] ) return false ; i ++ ; len -- ; } return true ; } static void insert ( TrieNode root , String key , int id ) { TrieNode pCrawl = root ; for ( int level = key . Length - 1 ; level >= 0 ; level -- ) { int index = key [ level ] - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = new TrieNode ( ) ; if ( isPalindrome ( key , 0 , level ) ) ( pCrawl . pos ) . Add ( id ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . id = id ; pCrawl . pos . Add ( id ) ; pCrawl . isLeaf = true ; } static List < List < int > > result ; static void search ( TrieNode root , String key , int id ) { TrieNode pCrawl = root ; for ( int level = 0 ; level < key . Length ; level ++ ) { int index = key [ level ] - ' a ' ; if ( pCrawl . id >= 0 && pCrawl . id != id && isPalindrome ( key , level , key . Length - 1 ) ) { List < int > l = new List < int > ( ) ; l . Add ( id ) ; l . Add ( pCrawl . id ) ; result . Add ( l ) ; } if ( pCrawl . children [ index ] == null ) return ; pCrawl = pCrawl . children [ index ] ; } foreach ( int i in pCrawl . pos ) { if ( i == id ) continue ; List < int > l = new List < int > ( ) ; l . Add ( id ) ; l . Add ( i ) ; result . Add ( l ) ; } } static Boolean checkPalindromePair ( List < String > vect ) { TrieNode root = new TrieNode ( ) ; for ( int i = 0 ; i < vect . Count ; i ++ ) insert ( root , vect [ i ] , i ) ; result = new List < List < int > > ( ) ; for ( int i = 0 ; i < vect . Count ; i ++ ) { search ( root , vect [ i ] , i ) ; if ( result . Count > 0 ) return true ; } return false ; } public static void Main ( String [ ] args ) { List < String > vect = new List < String > ( ) { " geekf " , " geeks " , " or " , " keeg " , " abc " , " bc " } ; if ( checkPalindromePair ( vect ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }