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

📅  最后修改于: 2021-09-06 06:25:06             🧑  作者: Mango

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

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

例子:

方法:思路是用动态规划来解决这个问题。
每个字符有两种可能的移动(前移或后移)。因此,对于每个字符,我们将检查是否可以在新字符串的前面或后面添加该字符。如果可能,我们将移至下一个字符。如果不可能,则操作将在该点停止并打印No。

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

下面是上述方法的实现

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


Javascript


输出:
Yes

时间复杂度: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live