Header Ads Widget

Swapping positions | hackerearth practice problem

  Swapping positions | hackerearth practice problem:-

  You are given two strings 

s and t of length n. You can select any two positions (can be from the same or different strings) and swap the characters at those two positions. You have to perform this operation exactly once in such a way that there exists maximum one position i (1in) such that si!=ti
Determine whether it is possible to perform the operation such that the given condition holds or not.

Input format

  • First line: A single integer T that denotes the number of test cases 
  • For each test case:
    • First line: An integer n 
    • Next two lines: Strings s and t respectively

Output format

For each test case, print YES if a valid operation exists otherwise print NO in a single line.

Input constraints

  • 1T50
  • 1n103
  • Both the strings s and t contain only lowercase English alphabets
Sample Input
Sample Output
Time Limit: 2
Memory Limit: 256
Source Limit:

In the first test case, we can get a valid result by swapping the characters s3 and t8 (1 based indexing).


Swapping positions | hackerearth practice problem Code:-

  Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also

Solution 1 ( C language):-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main(){
    int t, n;
    scanf("%d", &t);
    while(t-- > 0){
        scanf("%d", &n);
        char s1[n];
        char s2[n];
        scanf("%s %s", &s1, &s2);
        int mismatched = 0;
        bool swapped = false;
        bool potentialMatch = false;
        char char1 = '.';
        char char2 = '.';
        for(int i = 0; i < n; i++){
            if(s1[i] != s2[i]){
                if(char1 == '.' && char2 == '.'){
                    char1 = s1[i];
                    char2 = s2[i];
                else if(((char1 == s1[i] && char2 == s2[i])
|| (char2 == s1[i] && char1 == s2[i])) && !swapped){
                    mismatched -= 2;
                    swapped = true;
                else if(char1 == s1[i] || char2 == s2[i]){
                    potentialMatch = true;
        if(mismatched <= 1)
        else if(!swapped && potentialMatch && mismatched <= 2)

Solution 2 ( C++ language):-

 This solution is based on the c++ language and you can submit ib c++14 and c++17 also.
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..

This is your choice that you want to use this or not but in some cases the code may take more time in execution and that time we need it .

using namespace std;
#define FF first
#define SS second
#define PB push_back
#define VI vector<long long int>
#define mod 1000000007
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define MS1(X) memset((X), -1, sizeof((X)))
#define MS0(X) memset((X), 0, sizeof((X)))
int main()
    #ifndef ONLINE_JUDGE
    ll t=1;
    cin >> t;
        ll flag=0,n,i,j,count=0;
        cin >> n ;
        string s1,s2;
        cin >> s1 >> s2;
        vector<char> v1,v2;
        for(i=0;i<n;i++) {
            if(s1[i]!=s2[i]) {
        if(v1.size()<=1) {
            cout << "YES\n";
        } else if((v1.size()>3)) {
            cout << "NO\n";
        } else if(v1.size()==2) {
            if((v1[0]==v1[1])||(v2[0]==v2[1])||(v1[0]==v2[1])||(v1[1]==v2[0])) {
                cout << "YES\n";
            } else {
                cout << "NO\n";
        } else {
            for(i=0;i<3;i++) {
                for(j=0;j<3;j++) {
                    if(i!=j) {
&&(v2[j]==v2[i]))) {
                if(count==1) {
            if(count==1) {
                cout << "YES\n";
            } else {
                cout << "NO\n";


 ,sleeping positions to turn breech baby,

,sleeping positions,

,sleeping positions to induce labor,

,sleeping positions with a pacemaker,

,sleeping positions for lower back pain,

,sleeping positions to avoid after c-section,

,sleeping positions meaning,

,sleeping positions to relieve gas,

,sleeping positions while pregnant,

,sleeping positions for upper back pain,

,swapping array positions,

Recommended Post:

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-


Post a Comment