Problem:-
You are given a string 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 .
Input format
- The first line contains a single integer denoting the number of test cases. The description of test cases follows.
- Each line contains a string of lower case alphabets only.
Output format
For each test case, print the answer in a new line.
Constraints
contains only lowercase alphabets.
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
- In the first test case, you can create "bcp" which is not a palindrome and it is a lexicographically-smallest string.
- In the second test case, you cannot form any anti palindrome.
Code:-
in the starting i use three lines:-
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
this is only for reducing the time limit of the execution
Solution:-
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
string str1,str2;
while(t--)
{
cin>>str1;
str2=str1;
reverse(str1.begin(),str1.end());
if(str1==str2)
cout<<"-1"<<endl;
else
{
sort(str1.begin(),str1.end());
cout<<str1<<endl;
}
}
return 0;
}
Recommended post:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
0 Comments