Count of Palindromic Strings possible by swapping of a pair of Characters
Last Updated :
11 May, 2021
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: 2
Explanation:
1st Swap: abba -> abba
2nd 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(N3)
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 ith character, increase 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 ith character.
- Finally, after complete traversal of the string, print count.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
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) << '\n' ;
return 0;
}
|
Java
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));
}
}
|
Python3
def findNewString(s):
ans = 0
freq = [ 0 ] * 26
n = len (s)
for i in range (n):
ans + = freq[ ord (s[i]) - ord ( 'a' )]
freq[ ord (s[i]) - ord ( 'a' )] + = 1
return ans
s = "aaabaaa"
print (findNewString(s))
|
C#
using System;
class GFG{
static long findNewString(String s)
{
long ans = 0;
int []freq = new int [26];
int n = s.Length;
for ( int i = 0; i < ( int )s.Length; ++i)
{
ans += freq[s[i] - 'a' ];
freq[s[i] - 'a' ]++;
}
return ans;
}
public static void Main(String[] args)
{
String s = "aaabaaa" ;
Console.Write(findNewString(s));
}
}
|
Javascript
<script>
function findNewString(s) {
var ans = 0;
var freq = new Array(26).fill(0);
var n = s.length;
for (let i = 0; i < n; i++) {
ans += freq[s[i].charCodeAt(0) - "a" .charCodeAt(0)];
freq[s[i].charCodeAt(0) - "a" .charCodeAt(0)] += 1;
}
return ans;
}
var s = "aaabaaa" ;
document.write(findNewString(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...