Given two strings A and B of length N, the task is to check whether the two strings can be made equal by swapping any character of A with any other character of B only once.
Examples:
Input: A = “SEEKSFORGEEKS”, B = “GEEKSFORGEEKG”
Output: Yes
“SEEKSFORGEEKS” and “GEEKSFORGEEKG”
can be swapped to make both the strings equal.Input: A = “GEEKSFORGEEKS”, B = “THESUPERBSITE”
Output: No
Approach: First omit the elements which are the same and have the same index in both the strings. Then if the new strings are of length two and both the elements in each string are the same then only the swap is possible.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if the string // can be made equal after one swap bool canBeEqual(string a, string b, int n) { // A and B are new a and b // after we omit the same elements vector<char> A, B; // Take only the characters which are // different in both the strings // for every pair of indices for (int i = 0; i < n; i++) { // If the current characters differ if (a[i]!= b[i]) { A.push_back(a[i]); B.push_back(b[i]); } } // The strings were already equal if (A.size() == B.size() and B.size() == 0) return true; // If the lengths of the // strings are two if (A.size() == B.size() and B.size() == 2) { // If swapping these characters // can make the strings equal if (A[0] == A[1] and B[0] == B[1]) return true; } return false; } // Driver code int main() { string A = "SEEKSFORGEEKS"; string B = "GEEKSFORGEEKG"; if (canBeEqual(A, B, A.size())) printf("Yes"); else printf("No"); } // This code is contributed by Mohit Kumar |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if the string // can be made equal after one swap static boolean canBeEqual(char []a, char []b, int n) { // A and B are new a and b // after we omit the same elements Vector<Character> A = new Vector<>(); Vector<Character> B = new Vector<>(); // Take only the characters which are // different in both the strings // for every pair of indices for (int i = 0; i < n; i++) { // If the current characters differ if (a[i] != b[i]) { A.add(a[i]); B.add(b[i]); } } // The strings were already equal if (A.size() == B.size() && B.size() == 0) return true; // If the lengths of the // strings are two if (A.size() == B.size() && B.size() == 2) { // If swapping these characters // can make the strings equal if (A.get(0) == A.get(1) && B.get(0) == B.get(1)) return true; } return false; } // Driver code public static void main(String[] args) { char []A = "SEEKSFORGEEKS".toCharArray(); char []B = "GEEKSFORGEEKG".toCharArray(); if (canBeEqual(A, B, A.length)) System.out.printf("Yes"); else System.out.printf("No"); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach # Function that returns true if the string # can be made equal after one swap def canBeEqual(a, b, n): # A and B are new a and b # after we omit the same elements A =[] B =[] # Take only the characters which are # different in both the strings # for every pair of indices for i in range(n): # If the current characters differ if a[i]!= b[i]: A.append(a[i]) B.append(b[i]) # The strings were already equal if len(A)== len(B)== 0: return True # If the lengths of the # strings are two if len(A)== len(B)== 2: # If swapping these characters # can make the strings equal if A[0]== A[1] and B[0]== B[1]: return True return False # Driver code A = 'SEEKSFORGEEKS'B = 'GEEKSFORGEEKG' if (canBeEqual(A, B, len(A))): print("Yes") else: print("No") |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function that returns true if the string // can be made equal after one swap static Boolean canBeEqual(char []a, char []b, int n) { // A and B are new a and b // after we omit the same elements List<char> A = new List<char>(); List<char> B = new List<char>(); // Take only the characters which are // different in both the strings // for every pair of indices for (int i = 0; i < n; i++) { // If the current characters differ if (a[i] != b[i]) { A.Add(a[i]); B.Add(b[i]); } } // The strings were already equal if (A.Count == B.Count && B.Count == 0) return true; // If the lengths of the // strings are two if (A.Count == B.Count && B.Count == 2) { // If swapping these characters // can make the strings equal if (A[0] == A[1] && B[0] == B[1]) return true; } return false; } // Driver code public static void Main(String[] args) { char []A = "SEEKSFORGEEKS".ToCharArray(); char []B = "GEEKSFORGEEKG".ToCharArray(); if (canBeEqual(A, B, A.Length)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by PrinciRaj1992 |
Yes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Check whether two strings can be made equal by increasing prefixes
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check if two strings after processing backspace character are equal or not
- Minimum swaps to make two strings equal by swapping only with third string
- Check if a two character string can be made using given words
- Check whether the string S1 can be made equal to S2 with the given operation
- Number of character corrections in the given strings to make them equal
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Check if there is any common character in two given strings
- Check if frequency of each character is equal to its position in English Alphabet
- Palindrome by swapping only one character
- Lexicographically smaller string by swapping at most one character pair
- Find the character made by adding all the characters of the given string
- N-th character in the string made by concatenating natural numbers
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Count number of strings (made of R, G and B) using given combination
- Print all possible strings that can be made by placing spaces
- Print all possible strings that can be made by placing spaces
- Find the longest string that can be made up of other strings from the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

