Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Anti-palindrome strings problem solution

YASH PAL, 31 July 2024
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))
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes