I have written this code in starter30 but is giving me an error for after submission test cases.
Question is : SWAPCW
#include <iostream>
using namespace std;
bool isSorted(string s,int n){
for(int i = 0;i < n-1; i++){
if(s[i]>s[i+1]){
return false;
}
}
return true;
}
bool swap(string str,int n,int i){
char temp;
temp = str[i];
str[i] = str[n-1-i];
str[n-1-i] = temp;
return isSorted(str,n);
}
int main() {
// your code goes here
int t;
cin >> t;
int n;
string str;
while(t--){
cin >> n;
cin >> str;
bool ans;
int count = 0;
if(!isSorted(str,n)){
for(int i = 0;i < n/2;i++){
ans = swap(str,n,i);
if(ans){
cout << "YES" << endl;
break;
}
count++;
}
if(count==n/2){
cout << "NO" << endl;
}
}else
cout << "YES" << endl;
}
return 0;
}