Anti-palindrome strings

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.

Example:

Input:  s = "bpc"
Output: bcp

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string antiPalindrome(string s)
{
    sort(s.begin(), s.end());
    int n = s.size();
    if (s[0] == s[n - 1])
        return "-1";
    else
        return s;
}
int main()
{

    string s = "bpc";
    cout << antiPalindrome(s<< "\n";

    return 0;
}


No comments:

Post a Comment