检查是否可以通过相互交换一个字符来使两个字符串相等
给定两个长度为N的字符串A和B ,任务是通过仅将A的任何字符与B的任何其他字符交换一次来检查两个字符串是否相等。
例子:
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
方法:首先省略两个字符串中相同且具有相同索引的元素。然后,如果新字符串的长度为 2,并且每个字符串中的两个元素都相同,那么只有交换是可能的。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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 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 A = new Vector<>();
Vector 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 A = new List();
List B = new List();
// 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
Javascript
输出:
Yes