📌  相关文章
📜  检查是否可以使用给定的操作将一个字符串转换为其他字符串

📅  最后修改于: 2021-05-04 07:45:47             🧑  作者: Mango

给定两个相同长度的字符串ST。任务是确定是否可以通过执行以下操作来构建等于字符串T的字符串A(最初为空)。

  1. 删除S的第一个字符,并将其添加到A的前面。
  2. 删除S的第一个角色,并将其添加到A的后面。

例子:

方法:想法是使用动态编程来解决此问题。

每个字符有两种可能的移动方式(前移或后移)。因此,对于每个字符,我们将检查是否可以在新字符串的前面或后面添加字符。如果可能,我们将移至下一个字符。如果不可能,则该操作将在此时停止并打印“否”

  1. 首先,我们将创建一个二维布尔数组dp [] [],其行和列等于字符串S的长度,其中dp [i] [j] = 1表示从索引i到n-1的字符串S的所有字符都可以被放置在新的字符串A中,且前移j个,使其等于字符串T。
  2. 如果我们将第(i-1)个字符作为前移或第(i-1)个字符作为后移,则可以从后面遍历字符串S并以两种方式为每个字符更新dp [] []移动。
  3. 最后,我们将检查第一行的任何值是否等于一个。

下面是上述方法的实现

C++
// C++ implementation of above
// approach
#include 
using namespace std;
  
// Function that prints whether
// is it possible to make a
// string equal to T by
// performing given operations
void twoStringsEquality(string s,
                        string t)
{
    int n = s.length();
  
    vector > dp(
        n, vector(
               n + 1, 0));
  
    // Base case, if we put the
    // last character at front
    // of A
    if (s[n - 1] == t[0])
        dp[n - 1][1] = 1;
  
    // Base case, if we put the
    // last character at back
    // of A
    if (s[n - 1] == t[n - 1])
        dp[n - 1][0] = 1;
  
    for (int i = n - 1; i > 0; i--) {
        for (int j = 0; j <= n - i; j++) {
  
            // Condition if current
            // sequence is matchable
            if (dp[i][j]) {
                // Condition for front
                // move to (i - 1)th
                // character
                if (s[i - 1] == t[j])
                    dp[i - 1][j + 1] = 1;
  
                // Condition for back
                // move to (i - 1)th
                // character
                if (s[i - 1] == t[i + j - 1])
                    dp[i - 1][j] = 1;
            }
        }
    }
  
    bool ans = false;
  
    for (int i = 0; i <= n; i++) {
        // Condition if it is
        // possible to make
        // string A equal to
        // string T
        if (dp[0][i] == 1) {
            ans = true;
            break;
        }
    }
  
    // Print final
    // answer
    if (ans == true)
        cout << "Yes"
             << "\n";
    else
        cout << "No"
             << "\n";
}
  
// Driver Code
int main()
{
    string S = "abab";
  
    string T = "baab";
  
    twoStringsEquality(S, T);
  
    return 0;
}


Java
// Java implementation of above
// approach
import java.util.*;
  
class GFG{
  
// Function that prints whether
// is it possible to make a
// String equal to T by
// performing given operations
static void twoStringsEquality(String s,
                               String t)
{
    int n = s.length();
  
    int [][]dp = new int[n][n + 1];
      
    // Base case, if we put the
    // last character at front
    // of A
    if (s.charAt(n - 1) == t.charAt(0))
        dp[n - 1][1] = 1;
  
    // Base case, if we put the
    // last character at back
    // of A
    if (s.charAt(n - 1) == t.charAt(n - 1))
        dp[n - 1][0] = 1;
  
    for(int i = n - 1; i > 0; i--)
    {
       for(int j = 0; j <= n - i; j++)
       {
             
          // Condition if current
          // sequence is matchable
          if (dp[i][j] > 0)
          {
                
              // Condition for front
              // move to (i - 1)th
              // character
              if (s.charAt(i - 1) ==
                  t.charAt(j))
                  dp[i - 1][j + 1] = 1;
                
              // Condition for back
              // move to (i - 1)th
              // character
              if (s.charAt(i - 1) == 
                  t.charAt(i + j -1))
                  dp[i - 1][j] = 1;
          }
       }
    }
  
    boolean ans = false;
  
    for(int i = 0; i <= n; i++)
    {
          
       // Condition if it is possible
       // to make String A equal to
       // String T
       if (dp[0][i] == 1)
       {
           ans = true;
           break;
       }
    }
      
    // Print final answer
    if (ans == true)
        System.out.print("Yes" + "\n");
    else
        System.out.print("No" + "\n");
}
  
// Driver Code
public static void main(String[] args)
{
    String S = "abab";
    String T = "baab";
  
    twoStringsEquality(S, T);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of above
# approach
  
# Function that prints whether
# is it possible to make a
# equal to T by
# performing given operations
def twoStringsEquality(s, t):
    n = len(s)
  
    dp = [[0 for i in range(n + 1)]
             for i in range(n)]
  
    # Base case, if we put the
    # last character at front
    # of A
    if (s[n - 1] == t[0]):
        dp[n - 1][1] = 1
  
    # Base case, if we put the
    # last character at back
    # of A
    if (s[n - 1] == t[n - 1]):
        dp[n - 1][0] = 1
  
    for i in range(n - 1, -1, -1):
        for j in range(n - i + 1):
  
            # Condition if current
            # sequence is matchable
            if (dp[i][j]):
                  
                # Condition for front
                # move to (i - 1)th
                # character
                if (s[i - 1] == t[j]):
                    dp[i - 1][j + 1] = 1
  
                # Condition for back
                # move to (i - 1)th
                # character
                if (s[i - 1] == t[i + j - 1]):
                    dp[i - 1][j] = 1
  
    ans = False
  
    for i in range(n + 1):
          
        # Condition if it is
        # possible to make
        # A equal to T
        if (dp[0][i] == 1):
            ans = True
            break
  
    # Print final answer
    if (ans == True):
        print("Yes")
    else:
        print("No")
  
# Driver Code
if __name__ == '__main__':
      
    S = "abab"
    T = "baab"
  
    twoStringsEquality(S, T)
      
# This code is contributed by mohit kumar 29


C#
// C# implementation of above
// approach
using System;
class GFG{
  
// Function that prints whether
// is it possible to make a
// String equal to T by
// performing given operations
static void twoStringsEquality(String s,
                               String t)
{
    int n = s.Length;
  
    int [,]dp = new int[n, n + 1];
      
    // Base case, if we put the
    // last character at front
    // of A
    if (s[n - 1] == t[0])
        dp[n - 1, 1] = 1;
  
    // Base case, if we put the
    // last character at back
    // of A
    if (s[n - 1] == t[n - 1])
        dp[n - 1, 0] = 1;
  
    for(int i = n - 1; i > 0; i--)
    {
        for(int j = 0; j <= n - i; j++)
        {
                  
            // Condition if current
            // sequence is matchable
            if (dp[i, j] > 0)
            {
                      
                // Condition for front
                // move to (i - 1)th
                // character
                if (s[i - 1] == t[j]) 
                    dp[i - 1, j + 1] = 1;
                      
                // Condition for back
                // move to (i - 1)th
                // character
                if (s[i - 1] == t[i + j - 1])
                    dp[i - 1, j] = 1;
            }
        }
    }
      
    bool ans = false;
  
    for(int i = 0; i <= n; i++)
    {
          
        // Condition if it is possible
        // to make String A equal to
        // String T
        if (dp[0, i] == 1)
        {
            ans = true;
            break;
        }
    }
      
    // Print readonly answer
    if (ans == true)
        Console.Write("Yes" + "\n");
    else
        Console.Write("No" + "\n");
}
  
// Driver Code
public static void Main(String[] args)
{
    String S = "abab";
    String T = "baab";
  
    twoStringsEquality(S, T);
}
}
  
// This code is contributed by 29AjayKumar


输出:
Yes

时间复杂度: O(N 2 )