Make String Anti-Palindrome
Last Updated :
16 Feb, 2023
Given a string S of length N is said to be an Anti-palindrome string if, for each 0 ? i ? N-1, Si != S(N – 1 ? i). The task is to print Anti-Palindrome String formed, If it is not possible to find print “-1”
Examples:
Input: S = “abdcccdb”
Output: “cbbaccdd”
Explanation: cbbaccdd is an Anti-palindrome string as for each 0 ? i ? N-1, Si != S(N-1?i).
Input: s = “xyz”
Output: -1
Approach: The problem can be solved based on the following idea:
If the length of the string S is odd, Then the answer is always “-1” as the condition fails on the middle element. Count the frequency of each character in the string S, if any character frequency is more than half the length of the string, then also the condition fails. Otherwise, print the string having characters continuously up to their respective frequencies.
Follow the below steps to implement the idea:
- Check the length of the string, If it is odd print “-1“.
- Else, count the frequency of each character using the map.
- If any character frequency is more than half of the string S length, print “-1”.
- Else, print the string having characters continuously up to their respective frequencies.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string checkPalindrome(string s)
{
int n = s.size();
if (n & 1) {
return "-1" ;
}
map< char , int > Mp;
for ( int i = 0; i < n; i++)
Mp[s[i]]++;
string r = "" ;
for ( auto i : Mp) {
int p = i.second;
if (p > (n / 2)) {
return "-1" ;
}
for ( int j = 0; j < p; j++) {
r += i.first;
}
}
reverse(r.begin(), r.begin() + n / 2);
return r;
}
int main()
{
string s = "abdcccdb" ;
cout << checkPalindrome(s) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static String checkPalindrome(String s)
{
int n = s.length();
if (n % 2 == 1 ) {
return "-1" ;
}
Map<Character, Integer> Mp = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
Mp.put(s.charAt(i),
Mp.getOrDefault(s.charAt(i), 0 ) + 1 );
}
String r = "" ;
for (Map.Entry<Character, Integer> entry :
Mp.entrySet()) {
int p = entry.getValue();
if (p > (n / 2 )) {
return "-1" ;
}
for ( int j = 0 ; j < p; j++) {
r += entry.getKey();
}
}
r = new StringBuilder(r.substring( 0 , n / 2 ))
.reverse()
.toString()
+ r.substring(n / 2 );
return r;
}
public static void main(String[] args)
{
String s = "abdcccdb" ;
System.out.println(checkPalindrome(s));
}
}
|
Python3
def reverse(s):
str = ""
for i in s:
str = i + str
return str
def checkPalindrome(s):
n = len (s)
if n & 1 ! = 0 :
return "-1"
frequency = dict ()
for item in s:
if (item in frequency):
frequency[item] + = 1
else :
frequency[item] = 1
r = "";
for i in frequency:
p = frequency[i]
if (p > (n / / 2 )):
return "-1"
for j in range (p):
r + = i
print (reverse(r))
checkPalindrome( "abdcccdb" )
|
Javascript
function checkPalindrome(s) {
let n = s.length;
if (n & 1) {
return "-1" ;
}
let frequency = {};
for (let i = 0; i < n; i++) {
if (frequency[s[i]]) {
frequency[s[i]]++;
} else {
frequency[s[i]] = 1;
}
}
let r = "" ;
for (let i in frequency) {
let p = frequency[i];
if (p > (n / 2)) {
return "-1" ;
}
for (let j = 0; j < p; j++) {
r += i;
}
}
r = r.split( '' ).reverse().join( '' );
return r;
}
console.log(checkPalindrome( "abdcccdb" ));
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static string CheckPalindrome( string s)
{
int n = s.Length;
if (n % 2 == 1) {
return "-1" ;
}
Dictionary< char , int > Mp
= new Dictionary< char , int >();
for ( int i = 0; i < n; i++) {
if (!Mp.ContainsKey(s[i])) {
Mp.Add(s[i], 1);
}
else {
Mp[s[i]]++;
}
}
string r = "" ;
foreach (KeyValuePair< char , int > entry in Mp)
{
int p = entry.Value;
if (p > (n / 2)) {
return "-1" ;
}
for ( int j = 0; j < p; j++) {
r += entry.Key;
}
}
char [] charArray
= r.Substring(0, n / 2).ToCharArray();
Array.Reverse(charArray);
r = new string (charArray) + r.Substring(n / 2);
return r;
}
static void Main( string [] args)
{
string s = "abdcccdb" ;
Console.WriteLine(CheckPalindrome(s));
}
}
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Related Articles:
Share your thoughts in the comments
Please Login to comment...