Count of Palindromic Strings possible by swapping of a pair of Characters | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long findNewString ( string s ) { long long ans = 0 ; int freq [ 26 ] ; int n = s . length ( ) ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; ++ i ) { ans += freq [ s [ i ] - ' a ' ] ; freq [ s [ i ] - ' a ' ] ++ ; } return ans ; } int main ( ) { string s = " aaabaaa " ; cout << findNewString ( s ) << ' ' ; return 0 ; }