📌  相关文章
📜  检查两个字符串可以通过反转的一个字符串的子串是相等

📅  最后修改于: 2021-04-26 10:27:09             🧑  作者: Mango

给定两个长度为N的XY字符串,任务是通过将X的任何子字符串恰好反转一次来检查两个字符串是否相等。如果可能,请打印“是” 。否则,打印“否”

例子:

天真的方法:解决问题的最简单方法是反转字符串X的每个可能子字符串,对于每次反转,请检查两个字符串是否相等。如果在进行任何冲销后发现是正确的,则打印“是” 。否则,打印“否”。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:要优化上述方法,请按照以下步骤解决问题:

  • 初始化一个变量,将L表示-1 ,以从左开始存储两个字符串具有不相等字符的第一个索引。
  • 使用变量i[0,N – 1]范围内遍历字符串X ,如果对于任何索引,如果发现两个字符串中的字符不相等,则设置L = i并退出循环。
  • 初始化一个变量,将R表示-1 ,以从右开始存储两个字符串字符不相等的第一个索引。
  • 使用变量i[N – 1,0]范围内遍历字符串X ,如果对于任何索引,如果发现两个字符串中的字符不相等,则设置R = i并退出循环。
  • 在索引[L,R]上反转字符串X的字符。
  • 完成上述步骤后,检查两个字符串是否相等。如果发现相等,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the strings
// can be made equal or not by
// reversing a substring of X
bool checkString(string X, string Y)
{
    // Store the first index from
    // the left which contains unequal
    // characters in both the strings
    int L = -1;
 
    // Store the first element from
    // the right which contains unequal
    // characters in both the strings
    int R = -1;
 
    // Checks for the first index from
    // left in which characters in both
    // the strings are unequal
    for (int i = 0; i < X.length(); ++i) {
 
        if (X[i] != Y[i]) {
 
            // Store the current index
            L = i;
 
            // Break out of the loop
            break;
        }
    }
 
    // Checks for the first index from
    // right in which characters in both
    // the strings are unequal
    for (int i = X.length() - 1; i > 0; --i) {
        if (X[i] != Y[i]) {
 
            // Store the current index
            R = i;
 
            // Break out of the loop
            break;
        }
    }
 
    // Reverse the substring X[L, R]
    reverse(X.begin() + L,
            X.begin() + R + 1);
 
    // If X and Y are equal
    if (X == Y) {
        cout << "Yes";
    }
 
    // Otherwise
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    string X = "adcbef", Y = "abcdef";
 
    // Function Call
    checkString(X, Y);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if the Strings
// can be made equal or not by
// reversing a subString of X
static void checkString(String X, String Y)
{
    // Store the first index from
    // the left which contains unequal
    // characters in both the Strings
    int L = -1;
 
    // Store the first element from
    // the right which contains unequal
    // characters in both the Strings
    int R = -1;
 
    // Checks for the first index from
    // left in which characters in both
    // the Strings are unequal
    for (int i = 0; i < X.length(); ++i) {
 
        if (X.charAt(i) != Y.charAt(i)) {
 
            // Store the current index
            L = i;
 
            // Break out of the loop
            break;
        }
    }
 
    // Checks for the first index from
    // right in which characters in both
    // the Strings are unequal
    for (int i = X.length() - 1; i > 0; --i) {
        if (X.charAt(i) != Y.charAt(i)) {
 
            // Store the current index
            R = i;
 
            // Break out of the loop
            break;
        }
    }
 
    // Reverse the subString X[L, R]
    X = X.substring(0, L) +
      reverse(X.substring(L, R + 1)) +
      X.substring(R + 1);
 
    // If X and Y are equal
    if (X.equals(Y)) {
        System.out.print("Yes");
    }
 
    // Otherwise
    else {
        System.out.print("No");
    }
}
static String reverse(String input) {
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
    for (l = 0; l < r; l++, r--) {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.valueOf(a);
}
   
// Driver Code
public static void main(String[] args)
{
    String X = "adcbef", Y = "abcdef";
 
    // Function Call
    checkString(X, Y);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check if the strings
# can be made equal or not by
# reversing a substring of X
def checkString(X, Y):
 
    # Store the first index from
    # the left which contains unequal
    # characters in both the strings
    L = -1
 
    # Store the first element from
    # the right which contains unequal
    # characters in both the strings
    R = -1
 
    # Checks for the first index from
    # left in which characters in both
    # the strings are unequal
    for i in range(len(X)):
        if (X[i] != Y[i]):
 
            # Store the current index
            L = i
 
            # Break out of the loop
            break
 
    # Checks for the first index from
    # right in which characters in both
    # the strings are unequal
    for i in range(len(X) - 1, 0, -1):
        if (X[i] != Y[i]):
             
            # Store the current index
            R = i
 
            # Break out of the loop
            break
 
    X = list(X)
     
    X = X[:L] + X[R  : L - 1 : -1 ] + X[R + 1:]
     
    # If X and Y are equal
    if (X == list(Y)):
        print("Yes")
 
    # Otherwise
    else:
        print("No")
 
# Driver Code
if __name__ == "__main__" :
 
    X = "adcbef"
    Y = "abcdef"
 
    # Function Call
    checkString(X, Y)
     
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the Strings
// can be made equal or not by
// reversing a subString of X
static void checkString(String X, String Y)
{
     
    // Store the first index from
    // the left which contains unequal
    // characters in both the Strings
    int L = -1;
 
    // Store the first element from
    // the right which contains unequal
    // characters in both the Strings
    int R = -1;
 
    // Checks for the first index from
    // left in which characters in both
    // the Strings are unequal
    for(int i = 0; i < X.Length; ++i)
    {
        if (X[i] != Y[i])
        {
             
            // Store the current index
            L = i;
 
            // Break out of the loop
            break;
        }
    }
 
    // Checks for the first index from
    // right in which characters in both
    // the Strings are unequal
    for(int i = X.Length - 1; i > 0; --i)
    {
        if (X[i] != Y[i])
        {
             
            // Store the current index
            R = i;
 
            // Break out of the loop
            break;
        }
    }
 
    // Reverse the subString X[L, R]
    X = X.Substring(0, L) +
        reverse(X.Substring(L, R + 1 - L)) +
        X.Substring(R + 1);
 
    // If X and Y are equal
    if (X.Equals(Y))
    {
        Console.Write("Yes");
    }
 
    // Otherwise
    else
    {
        Console.Write("No");
    }
}
 
static String reverse(String input)
{
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.Join("",a);
}
   
// Driver Code
public static void Main(String[] args)
{
    String X = "adcbef", Y = "abcdef";
 
    // Function Call
    checkString(X, Y);
}
}
 
// This code is contributed by Amit Katiyar


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)