Swapping Chefs Way Solution || Codechef Starters - 30

             

Swapping Chefs Way Solution 

Codechef Starters - 30
Problem Code : SWAPCW

Solution :

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;

void solve()
{
    ll n;
    string s;
    cin >> n >> s;
    for (ll i = 0; i < n / 2; i++)
    {
        if (s[i] > s[n - i - 1])
        {
            swap(s[i], s[n - i - 1]);
        }
    }
    bool ans = true;
    for (ll i = 1; i < n; i++)
    {
        if (s[i] >= s[i - 1])
        {
            ans = true;
        }
        else
        {
            ans = false;
            break;
        }
    }
    if (ans)
    {
        cout << "YES\n";
    }
    else
    {
        cout << "NO\n";
    }
}

int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

Comments