Check if two strings can become same after one swap (original) (raw)
Last Updated : 20 May, 2026
Given two strings, the task is to check whether these strings can be made equal by exactly **one swap in any of the 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: True
**Explanation: By just swapping 'v' and 's' in any of string, both will become same.**Input: s1 = "abc", s2 = "abc"
**Output: False
**Explanation: They are already same
Table of Content
- [Naive Approach] Using Brute Force - O(n^3) Time O(n) Space
- [Expected Approach] Using Two Pointers - O(n) Time and O(1) Space
[Naive Approach] Using Brute Force - O(n^3) Time O(n) Space
The idea is to try all possible pairs of indices
(i, j)in one string and swap the characters at these positions. After each swap, compare the modified string with the second string. If they become equal, return true; otherwise, revert the swap and continue. If no swap makes the strings equal, return false.
C++ `
#include <bits/stdc++.h> using namespace std;
bool metaStrings(string &s1, string &s2) { // Length must be same if (s1.size() != s2.size()) return false;
// Equal strings are not meta strings
if (s1 == s2)
return false;
int n = s1.size();
// Try all possible swaps
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
string temp = s1;
// Swap characters
swap(temp[i], temp[j]);
// Check if equal
if (temp == s2)
return true;
}
}
return false;}
// Driver Code int main() { string s1 = "converse"; string s2 = "conserve";
if (metaStrings(s1, s2))
cout << "true";
else
cout << "false";
return 0;}
C
#include <stdbool.h> #include <stdio.h> #include <string.h>
bool metaStrings(char *s1, char *s2) { // Length must be same if (strlen(s1) != strlen(s2)) return false;
// Equal strings are not meta strings
if (strcmp(s1, s2) == 0)
return false;
int n = strlen(s1);
// Try all possible swaps
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
char temp = s1[i];
// Swap characters
s1[i] = s1[j];
s1[j] = temp;
// Check if equal
if (strcmp(s1, s2) == 0)
return true;
// Swap back
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
}
}
return false;}
// Driver Code int main() { char s1[] = "converse"; char s2[] = "conserve";
if (metaStrings(s1, s2))
printf("true");
else
printf("false");
return 0;}
Java
public class GfG { public static boolean metaStrings(String s1, String s2) { // Length must be same if (s1.length() != s2.length()) return false;
// Equal strings are not meta strings
if (s1.equals(s2))
return false;
int n = s1.length();
// Try all possible swaps
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
char[] temp = s1.toCharArray();
// Swap characters
char t = temp[i];
temp[i] = temp[j];
temp[j] = t;
// Check if equal
if (new String(temp).equals(s2))
return true;
}
}
return false;
}
// Driver Code
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
def metaStrings(s1, s2): # Length must be same if len(s1) != len(s2): return False
# Equal strings are not meta strings
if s1 == s2:
return False
n = len(s1)
# Try all possible swaps
for i in range(n):
for j in range(i + 1, n):
temp = list(s1)
# Swap characters
temp[i], temp[j] = temp[j], temp[i]
temp = ''.join(temp)
# Check if equal
if temp == s2:
return True
return FalseDriver Code
if name == "main": s1 = "converse" s2 = "conserve"
if metaStrings(s1, s2):
print("true")
else:
print("false")C#
using System;
public class GfG { public static bool MetaStrings(string s1, string s2) { // Length must be same if (s1.Length != s2.Length) return false;
// Equal strings are not meta strings
if (s1 == s2)
return false;
int n = s1.Length;
// Try all possible swaps
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
char[] temp = s1.ToCharArray();
// Swap characters
char t = temp[i];
temp[i] = temp[j];
temp[j] = t;
// Check if equal
if (new string(temp) == s2)
return true;
}
}
return false;
}
// Driver Code
public static void Main()
{
string s1 = "converse";
string s2 = "conserve";
if (MetaStrings(s1, s2))
Console.WriteLine("true");
else
Console.WriteLine("false");
}}
JavaScript
function metaStrings(s1, s2) { // Length must be same if (s1.length !== s2.length) return false;
// Equal strings are not meta strings
if (s1 === s2)
return false;
let n = s1.length;
// Try all possible swaps
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let temp = s1.split("");
// Swap characters
[temp[i], temp[j]] = [ temp[j], temp[i] ];
// Check if equal
if (temp.join("") === s2)
return true;
}
}
return false;}
// Driver Code let s1 = "converse"; let s2 = "conserve";
if (metaStrings(s1, s2)) console.log("true"); else console.log("false");
`
**Time Complexity: O(n^3)
**Auxiliary Space: O(n)
[Expected 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++ `
#include #include
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];}
// Driver Code int main() { string s1 = "geeks"; string s2 = "keegs";
bool ans = metaStrings(s1, s2);
cout << (ans ? "true" : "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");
`
**Time Complexity: O(n)
**Auxiliary Space: O(1)