Count substrings of a given string whose anagram is a palindrome | Java program for the above approach ; Function to get the count of subStrings whose anagrams are palindromic ; Store the answer ; Map to store the freq of masks ; Set frequency for mask 00. . .00 to 1 ; Store mask in x from 0 to i ; Update answer ; Update frequency ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static void countSubString ( String s ) { int answer = 0 ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; m . put ( 0 , 1 ) ; int x = 0 ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { x ^= 1 << ( s . charAt ( j ) - ' a ' ) ; answer += m . containsKey ( x ) ? m . get ( x ) : 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { answer += m . containsKey ( x ^ ( 1 << i ) ) ? m . get ( x ^ ( 1 << i ) ) : 0 ; } if ( m . containsKey ( x ) ) m . put ( x , m . get ( x ) + 1 ) ; else m . put ( x , 1 ) ; } System . out . print ( answer ) ; } public static void main ( String [ ] args ) { String str = " abab " ; countSubString ( str ) ; } }