Open In App

Meta Strings (Check if two strings can become same after a swap in one string)

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are not considered here as Meta strings.

Examples: 

Input: s1 = "geeks" , s2 = "keegs"
Output: True
Explanation: By just swapping 'k' and 'g' in any of string, both will become same.

Input: s1 = "Converse", s2 = "Conserve"
Output: False
Explanation: By just swapping 'v' and 's' in any of string, both will become same.

Approach: Using Two Pointers - O(n) Time and O(1) Space

The idea is to find exactly two mismatched positions in the strings and check if swapping them makes the strings identical. We iterate once, tracking mismatched indices using two variables. If there are exactly two mismatches and swapping them results in equality, return True; otherwise, return False.
C++
// C++ program to check if two strings
// are meta strings using two pointers
#include <iostream>
#include <string>

using namespace std;

bool metaStrings(string &s1, string &s2) {
  
    // If lengths are not equal, return false
    if (s1.size() != s2.size()) {
        return false;
    }

    // To track indices of mismatched characters
    int first = -1, second = -1;
    int count = 0;

    for (int i = 0; i < s1.size(); i++) {
        if (s1[i] != s2[i]) {
            if (count == 0) {
                first = i;
            } else if (count == 1) {
                second = i;
            } else {
                
                // More than 2 mismatches
                return false; 
            }
            count++;
        }
    }

    // There must be exactly 2 mismatches
    // and they should be swappable
    return count == 2 && s1[first] == s2[second]
    && s1[second] == s2[first];
}

int main() {
    string s1 = "converse";
    string s2 = "conserve";

    if(metaStrings(s1, s2))
    cout<<"True";
    else cout<<"False";
    return 0;
}
Java
// Java program to check if two strings
// are meta strings using two pointers
class GfG {
    
    static boolean metaStrings(String s1, String s2) {
        
        // If lengths are not equal, return false
        if (s1.length() != s2.length()) {
            return false;
        }

        // To track indices of mismatched characters
        int first = -1, second = -1, count = 0;

        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i)) {
                if (count == 0) {
                    first = i;
                } else if (count == 1) {
                    second = i;
                } else {
                    return false; // More than 2 mismatches
                }
                count++;
            }
        }

        // There must be exactly 2 mismatches 
        // and they should be swappable
        return count == 2 && s1.charAt(first) == s2.charAt(second) 
                         && s1.charAt(second) == s2.charAt(first);
    }

    public static void main(String[] args) {
        String s1 = "converse";
        String s2 = "conserve";

         if(metaStrings(s1, s2))
          System.out.println("True");
          else System.out.println("False");
    }
}
Python
# Python program to check if two string 
# are meta string using two pointers

def metaStrings(s1, s2):
    # If lengths are not equal, return False
    if len(s1) != len(s2):
        return False

    # To track indices of mismatched characters
    first, second, count = -1, -1, 0

    for i in range(len(s1)):
        if s1[i] != s2[i]:
            if count == 0:
                first = i
            elif count == 1:
                second = i
            else:
                # More than 2 mismatches
                return False  
            count += 1

    # There must be exactly 2 
    # mismatches and they should be swappable
    return count == 2 and s1[first] == s2[second]and s1[second] == s2[first]

if __name__ == "__main__":
    s1 = "converse"
    s2 = "conserve"

    print("True" if metaStrings(s1, s2) else "False")
   
C#
// C# program to check if two strings are meta strings
// using two pointers
using System;

class GfG {
    static bool metaStrings(string s1, string s2) {
        
        // If lengths are not equal, return false
        if (s1.Length != s2.Length) {
            return false;
        }

        // To track indices of mismatched characters
        int first = -1, second = -1, count = 0;

        for (int i = 0; i < s1.Length; i++) {
            if (s1[i] != s2[i]) {
                if (count == 0) {
                    first = i;
                } else if (count == 1) {
                    second = i;
                } else {
                    
                    // More than 2 mismatches
                    return false; 
                }
                count++;
            }
        }

        // There must be exactly 2 mismatches
        // and they should be swappable
        return count == 2 && s1[first] == s2[second] 
                         && s1[second] == s2[first];
    }

    public static void Main() {
        string s1 = "converse";
        string s2 = "conserve";

        Console.WriteLine(metaStrings(s1, s2) 
        ? "True" : "False");
    }
}
JavaScript
// Javascript program to check if two strings are meta strings
// using two pointers

// Function to check if two strings are meta strings
function metaStrings(s1, s2) {
    
    // If lengths are not equal, return false
    if (s1.length !== s2.length) {
        return false;
    }

    // To track indices of mismatched characters
    let first = -1, second = -1, count = 0;

    for (let i = 0; i < s1.length; i++) {
        if (s1[i] !== s2[i]) {
            if (count === 0) {
                first = i;
            } else if (count === 1) {
                second = i;
            } else {
                // More than 2 mismatches
                return false; 
            }
            count++;
        }
    }

    // There must be exactly 2 mismatches 
    // and they should be swappable
    return count === 2 && s1[first] === s2[second]
    && s1[second] === s2[first];
}

// Driver code
let s1 = "converse";
let s2 = "conserve";

console.log(metaStrings(s1, s2) ? "True" : "False");

Output
True

 


Article Tags :
Practice Tags :

Similar Reads