Count of Palindromic Strings possible by swapping of a pair of Characters | Java Program to implement the above approach ; Function to return the count of possible palindromic Strings ; Stores the frequencies of each character ; Stores the length of the String ; Increase the number of swaps , the current character make with its previous occurrences ; Increase frequency ; Driver Code
import java . util . * ; class GFG { static long findNewString ( String s ) { long ans = 0 ; int [ ] freq = new int [ 26 ] ; int n = s . length ( ) ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; ++ i ) { ans += freq [ s . charAt ( i ) - ' a ' ] ; freq [ s . charAt ( i ) - ' a ' ] ++ ; } return ans ; } public static void main ( String [ ] args ) { String s = " aaabaaa " ; System . out . print ( findNewString ( s ) ) ; } }