📌  相关文章
📜  根据给定条件检查两个字符串是否相等

📅  最后修改于: 2021-05-04 20:22:25             🧑  作者: Mango

给定两个大小相等的字符串AB。以下条件之一成立的两个字符串是等效的:
1)他们都是平等的。或者,
2)如果我们将字符串A分为两个大小相同的A 1和A 2的连续子字符串,并且将字符串B分成大小相同的B 1和B 2的两个连续子字符串,那么下列其中一项应该是正确的:

  • A 1递归等效于B 1,而A 2递归等效于B 2
  • A 1递归等效于B 2,而A 2递归等效于B 1

检查给定的字符串是否等效。打印是或否。

例子:

天真的解决方案:一个简单的解决方案是考虑所有可能的情况。首先检查两个字符串是否相等,返回“ YES”,否则对字符串分割,并使用四个递归调用检查A 1 = B 1A 2 = B 2还是A 1 = B 2A 2 = B 1 。该解决方案的复杂度为O(n 2 ),其中n是字符串的大小。

高效的解决方案:让我们在字符串S上定义以下操作。我们可以将其分为两半,如果需要,可以交换它们。而且,我们可以递归地将此操作应用于其两个部分。通过仔细观察,我们可以看到,如果对某个字符串A进行运算后,我们可以获得B,那么对B进行运算后,我们可以获得A。对于给定的两个字符串,我们可以递归地找到最小词典顺序的字符串。可以从他们那里获得。那些获得的字符串是否相等,答案为是,否则为否。例如,“ aaba”的最小词典字符串是“ aaab”。至少在字典上,“ abaa”的字符串也是“ aaab”。因此,这两个都是等效的。

下面是上述方法的实现。

C++
// CPP Program to find whether two strings
// are equivalent or not according to given
// condition
#include 
using namespace std;
  
// This function returns the least lexicogr
// aphical string obtained from its two halves
string leastLexiString(string s)
{
    // Base Case - If string size is 1
    if (s.size() & 1)
        return s;
  
    // Divide the string into its two halves
    string x = leastLexiString(s.substr(0,
                                        s.size() / 2));
    string y = leastLexiString(s.substr(s.size() / 2));
  
    // Form least lexicographical string
    return min(x + y, y + x);
}
  
bool areEquivalent(string a, string b)
{
  return (leastLexiString(a) == leastLexiString(b));
} 
  
// Driver Code
int main()
{
    string a = "aaba";
    string b = "abaa";
    if (areEquivalent(a, b))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
  
    a = "aabb";
    b = "abab";
    if (areEquivalent(a, b))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}


Java
// Java Program to find whether two strings
// are equivalent or not according to given
// condition
class GfG 
{
  
// This function returns the least lexicogr
// aphical String obtained from its two halves
static String leastLexiString(String s)
{
    // Base Case - If String size is 1
    if (s.length() == 1)
        return s;
  
    // Divide the String into its two halves
    String x = leastLexiString(s.substring(0,
                                        s.length() / 2));
    String y = leastLexiString(s.substring(s.length() / 2));
  
    // Form least lexicographical String
    return String.valueOf((x + y).compareTo(y + x));
}
  
static boolean areEquivalent(String a, String b)
{
    return !(leastLexiString(a).equals(leastLexiString(b)));
} 
  
// Driver Code
public static void main(String[] args) 
{
    String a = "aaba";
    String b = "abaa";
    if (areEquivalent(a, b))
        System.out.println("Yes");
    else
        System.out.println("No");
  
    a = "aabb";
    b = "abab";
    if (areEquivalent(a, b))
        System.out.println("Yes");
    else
        System.out.println("No");
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python 3 Program to find whether two strings
# are equivalent or not according to given
# condition
  
# This function returns the least lexicogr
# aphical string obtained from its two halves
def leastLexiString(s):
      
    # Base Case - If string size is 1
    if (len(s) & 1 != 0):
        return s
  
    # Divide the string into its two halves
    x = leastLexiString(s[0:int(len(s) / 2)])
    y = leastLexiString(s[int(len(s) / 2):len(s)])
  
    # Form least lexicographical string
    return min(x + y, y + x)
  
def areEquivalent(a,b):
    return (leastLexiString(a) == leastLexiString(b))
  
# Driver Code
if __name__ == '__main__':
    a = "aaba"
    b = "abaa"
    if (areEquivalent(a, b)):
        print("YES")
    else:
        print("NO")
  
    a = "aabb"
    b = "abab"
    if (areEquivalent(a, b)):
        print("YES")
    else:
        print("NO")
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# Program to find whether two strings 
// are equivalent or not according to given 
// condition 
using System;
class GFG 
{ 
  
// This function returns the least lexicogr- 
// aphical String obtained from its two halves 
static String leastLexiString(String s) 
{ 
    // Base Case - If String size is 1 
    if (s.Length == 1) 
        return s; 
  
    // Divide the String into its two halves 
    String x = leastLexiString(s.Substring(0, 
                               s.Length / 2)); 
    String y = leastLexiString(s.Substring(
                               s.Length / 2)); 
  
    // Form least lexicographical String 
    return ((x + y).CompareTo(y + x).ToString()); 
} 
  
static Boolean areEquivalent(String a, String b) 
{ 
    return !(leastLexiString(a).Equals(
             leastLexiString(b))); 
} 
  
// Driver Code 
public static void Main(String[] args) 
{ 
    String a = "aaba"; 
    String b = "abaa"; 
    if (areEquivalent(a, b)) 
        Console.WriteLine("YES"); 
    else
        Console.WriteLine("NO"); 
  
    a = "aabb"; 
    b = "abab"; 
    if (areEquivalent(a, b)) 
        Console.WriteLine("YES"); 
    else
        Console.WriteLine("NO"); 
} 
} 
  
// This code is contributed by PrinciRaj1992


PHP


输出:
YES
NO

时间复杂度: O(n),其中n是字符串的大小。