Given a palindromic string **S**, the task is to find the count of palindromic strings possible by swapping a pair of character at a time.**Examples:**

Input:s = “abba”Output:2Explanation:

1^{st}Swap:abba->abba

2^{nd}Swap: abba -> abba

All other swaps will lead to a non-palindromic string.

Therefore, the count of possible strings is 2.

Input:s = “aaabaaa”Output:15

**Naive Approach:**

The simplest approach to solve the problem is to generate all possible pair of characters from the given string and for each pair if swapping them generates a palindromic string or not. If found to be true, increase **count**. Finally, print the value of **count**.

**Time Complexity:** O(N^{3}) **Auxiliary Space:** O(1)

**Efficient Approach:**

To optimize the above-mentioned approach, calculate the **frequencies** of each character in the string. For the string to remain a palindrome, only the same character can be swapped in the string.

Follow the steps below to solve the problem:

- Traverse the string.
- For every
**i**, increase^{th}character**count**with the current frequency of the character. This increases the number of swaps the current character can make with its previous occurrences. - Increase the frequency of the
**i**.^{th}character - Finally, after complete traversal of the string, print
**count**.

Below is the implementation of above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// possible palindromic strings ` `long` `long` `findNewString(string s) ` `{ ` ` ` ` ` `long` `long` `ans = 0; ` ` ` ` ` `// Stores the frequencies ` ` ` `// of each character ` ` ` `int` `freq[26]; ` ` ` ` ` `// Stores the length of ` ` ` `// the string ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Initialize frequencies ` ` ` `memset` `(freq, 0, ` `sizeof` `freq); ` ` ` ` ` `for` `(` `int` `i = 0; i < (` `int` `)s.length(); ++i) { ` ` ` ` ` `// Increase the number of swaps, ` ` ` `// the current character make with ` ` ` `// its previous occurrences ` ` ` `ans += freq[s[i] - ` `'a'` `]; ` ` ` ` ` `// Increase frequency ` ` ` `freq[s[i] - ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string s = ` `"aaabaaa"` `; ` ` ` `cout << findNewString(s) << ` `'\n'` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to return the count of ` `# possible palindromic strings ` `def` `findNewString(s): ` ` ` ` ` `ans ` `=` `0` ` ` ` ` `# Stores the frequencies ` ` ` `# of each character ` ` ` `freq ` `=` `[` `0` `] ` `*` `26` ` ` ` ` `# Stores the length of ` ` ` `# the string ` ` ` `n ` `=` `len` `(s) ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Increase the number of swaps, ` ` ` `# the current character make with ` ` ` `# its previous occurrences ` ` ` `ans ` `+` `=` `freq[` `ord` `(s[i]) ` `-` `ord` `(` `'a'` `)] ` ` ` ` ` `# Increase frequency ` ` ` `freq[` `ord` `(s[i]) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` `s ` `=` `"aaabaaa"` ` ` `print` `(findNewString(s)) ` ` ` `# This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to implement ` `// the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to return the count of ` `// possible palindromic Strings ` `static` `long` `findNewString(String s) ` `{ ` ` ` `long` `ans = 0; ` ` ` ` ` `// Stores the frequencies ` ` ` `// of each character ` ` ` `int` `[]freq = ` `new` `int` `[26]; ` ` ` ` ` `// Stores the length of ` ` ` `// the String ` ` ` `int` `n = s.Length; ` ` ` ` ` `for` `(` `int` `i = 0; i < (` `int` `)s.Length; ++i) ` ` ` `{ ` ` ` ` ` `// Increase the number of swaps, ` ` ` `// the current character make with ` ` ` `// its previous occurrences ` ` ` `ans += freq[s[i] - ` `'a'` `]; ` ` ` ` ` `// Increase frequency ` ` ` `freq[s[i] - ` `'a'` `]++; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String s = ` `"aaabaaa"` `; ` ` ` `Console.Write(findNewString(s)); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

15

**Time Complexity:** O(N) **Auxiliary Space:** O(N)

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:

- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Print all possible palindromic string formed using any pair of given strings
- Largest palindromic string possible from given strings by rearranging the characters
- Count of non-palindromic strings of length M using given N characters
- Count the Number of matching characters in a pair of strings
- Count of Distinct strings possible by inserting K characters in the original string
- Find a pair of elements swapping which makes sum of two arrays same
- Lexicographically smaller string by swapping at most one character pair
- 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
- Replace minimal number of characters to make all characters pair wise distinct
- Reduce Hamming distance by swapping two characters
- Maximum length of balanced string after swapping and removal of characters
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if it is possible to make two martices strictly increasing by swapping corresponding values only
- Smallest number possible by swapping adjacent even odd pairs
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Count of unique palindromic strings of length X from given string

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.