In this HackerEarth Anti-palindrome strings problem solution you are given a string S containing only lowercase alphabets. You can swap two adjacent characters any number of times (including 0).

A string is called anti-palindrome if it is not a palindrome. If it is possible to make a string anti-palindrome, then find the lexicographically smallest anti-palindrome. Otherwise, print -1.


HackerEarth Anti-palindrome strings problem solution in java python c++ c and javascript programming with practical program code example and explanation


HackerEarth Anti-palindrome strings problem solution.

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 200005;
#define M 1000000007
#define double long double
#define BINF 1000000000000001
const int minN = 1e7 + 5;
#define init(arr,val) memset(arr,val,sizeof(arr))
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
 

#undef int 
int main() {
#define int long long int
ios_base::sync_with_stdio(false); 
cin.tie(0); 
cout.tie(0);
 
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("optput.txt", "w", stdout); 
#endif

    
    int t;
    cin >> t;
    while(t--){
      string s;
      cin >> s;
      sort(s.begin(), s.end());
      if(s[0] == s[s.length() - 1]){
        cout << -1 << endl;
      }else{
        cout << s << endl;
      }
    }
    
 

return 0;  
}

second solution

t = int(input())
while t > 0:
	t -= 1
	s = list(input())
	s.sort()
	if s[0] == s[-1]:
		print(-1)
	else:
		print(''.join(s))