📜  其中,当单独地移除使得字符串的字符串的字符计数等于另一个字符串

📅  最后修改于: 2021-10-26 06:51:36             🧑  作者: Mango

分别给定大小为NM 的两个字符串AB ,任务是计算字符串A 的字符数,当单独删除时,这两个字符串相等。如果存在多个这样的字符,则打印它们各自的位置。否则,打印“-1”

例子:

方法:根据以下观察可以解决给定的问题:

请按照以下步骤解决问题:

  • 初始化两个变量,比如X0YN-1来存储最长等前缀字符串的结束索引和最长等后缀字符串的开始索引。
  • 迭代过的字符串B的字符,然后在每次迭代中检查当前的字符在字符串A的索引X,然后通过递增1 X等于字符。否则,断。
  • 迭代过的字符串B的反转的字符,然后在每次迭代中检查,如果当前字符是等于在字符串A的指数Y上的字符,然后减1。否则,断。
  • 现在,如果NM之间的差值等于1并且Y小于X,则:
    • 打印X-Y+1等字符的总数
    • 然后通过迭代范围[Y+1, X+1] 来打印字符的索引
  • 否则,打印“-1”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count characters
// from string A whose removal
// makes the strings A and B equal
void RemoveOneChar(string A, string B,
                   int N, int M)
{
    // Stores the index of
    // the longest prefix
    int X = 0;
 
    // Stores the index of
    // the longest suffix
    int Y = N - 1;
 
    // Traverse the string B
    for (int i = 0; i < M; i++) {
        if (A[X] != B[i])
            break;
        X++;
    }
 
    // Traverse the string B
    for (int i = M - 1; i >= 0; i--) {
        if (A[Y] != B[i])
            break;
        Y--;
    }
    // If N - M is equal to 1 and Y
    // is less than or equal to X
    if (N - M == 1 && Y < X) {
 
        // Print the count
        // of characters
        cout << X - Y + 1 << endl;
 
        // Print the positions
        // of the characters
        for (int i = Y; i <= X; i++)
            cout << i + 1 << " ";
        cout << endl;
    }
 
    // Otherwise
    else
        cout << -1 << endl;
}
 
// Driver Code
int main()
{
 
    string A = "abaac";
    string B = "abac";
    int N = A.length();
    int M = B.length();
 
    RemoveOneChar(A, B, N, M);
}


Java
// Java program for the above approach
class GFG{
     
// Function to count characters
// from string A whose removal
// makes the strings A and B equal
static void RemoveOneChar(String A, String B,
                          int N, int M)
{
     
    // Stores the index of
    // the longest prefix
    int X = 0;
 
    // Stores the index of
    // the longest suffix
    int Y = N - 1;
 
    // Traverse the string B
    for(int i = 0; i < M; i++)
    {
        if (A.charAt(X) != B.charAt(i))
            break;
             
        X++;
    }
 
    // Traverse the string B
    for(int i = M - 1; i >= 0; i--)
    {
        if (A.charAt(Y) != B.charAt(i))
            break;
             
        Y--;
    }
     
    // If N - M is equal to 1 and Y
    // is less than or equal to X
    if (N - M == 1 && Y < X)
    {
         
        // Print the count
        // of characters
        System.out.println(X - Y + 1);
 
        // Print the positions
        // of the characters
        for(int i = Y; i <= X; i++)
            System.out.print(i + 1 + " ");
             
        System.out.println();
    }
 
    // Otherwise
    else
        System.out.println(-1);
}
 
// Driver Code
static public void main(String []args)
{
    String A = "abaac";
    String B = "abac";
    int N = A.length();
    int M = B.length();
 
    RemoveOneChar(A, B, N, M);
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to count characters
# from A whose removal
# makes the strings A and B equal
def RemoveOneChar(A, B, N, M):
     
    # Stores the index of
    # the longest prefix
    X = 0
 
    # Stores the index of
    # the longest suffix
    Y = N - 1
 
    # Traverse the B
    for i in range(M):
        if (A[X] != B[i]):
            break
         
        X += 1
 
    # Traverse the B
    for i in range(M - 1, -1, -1):
        if (A[Y] != B[i]):
            break
         
        Y -= 1
         
    # If N - M is equal to 1 and Y
    # is less than or equal to X
    if (N - M == 1 and Y < X):
 
        # Prthe count
        # of characters
        print(X - Y + 1)
 
        # Prthe positions
        # of the characters
        for i in range(Y, X + 1):
            print(i + 1, end = " ")
             
        print()
 
    # Otherwise
    else:
        print(-1)
 
# Driver Code
if __name__ == '__main__':
     
    A = "abaac"
    B = "abac"
    N = len(A)
    M = len(B)
 
    RemoveOneChar(A, B, N, M)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
 
using System;
 
public class GFG{
     
    // Function to count characters
    // from string A whose removal
    // makes the strings A and B equal
    static void RemoveOneChar(string A, string B,
                       int N, int M)
    {
        // Stores the index of
        // the longest prefix
        int X = 0;
     
        // Stores the index of
        // the longest suffix
        int Y = N - 1;
     
        // Traverse the string B
        for (int i = 0; i < M; i++) {
            if (A[X] != B[i])
                break;
            X++;
        }
     
        // Traverse the string B
        for (int i = M - 1; i >= 0; i--) {
            if (A[Y] != B[i])
                break;
            Y--;
        }
        // If N - M is equal to 1 and Y
        // is less than or equal to X
        if (N - M == 1 && Y < X) {
     
            // Print the count
            // of characters
            Console.WriteLine(X - Y + 1);
     
            // Print the positions
            // of the characters
            for (int i = Y; i <= X; i++)
                Console.Write(i + 1 + " ");
            Console.WriteLine();
        }
     
        // Otherwise
        else
            Console.WriteLine(-1) ;
    }
     
    // Driver Code
    static public void Main (){
         
        string A = "abaac";
        string B = "abac";
        int N = A.Length;
        int M = B.Length;
     
        RemoveOneChar(A, B, N, M);
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
2
3 4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程