分别给定大小为N和M 的两个字符串A和B ,任务是计算字符串A 的字符数,当单独删除时,这两个字符串相等。如果存在多个这样的字符,则打印它们各自的位置。否则,打印“-1” 。
例子:
Input: A = “abaac”, B = “abac”
Output: 2
3 4
Explanation:
Following removals are possible that can make the strings equal:
- Removing A[2] modifies A to “abac”, which becomes equal to B.
- Removing A[3] modifies A to “abac”, which becomes equal to B.
Therefore, two possible removals satisfy the conditions.
Input: A = “abs”, B = “bkk”
Output: -1
方法:根据以下观察可以解决给定的问题:
- Suppose, if removing the character at index i, makes both strings equal then the prefix of the string i.e substring over the range [0, i-1] must be equal and the suffix of the string i.e substring over the range [i+1, N-1] must be equal too.
- Suppose i is the index satisfying that the substring over the range [0, i-1] is the longest equal prefix string. And j is the index satisfying that the substring over the range [j+1, N-1] is the longest equal suffix string
- Then, if i>=j then only, there exist characters removing which makes the both strings equal. And total count of such characters removing which individually makes the strings equal is i-j+1.
请按照以下步骤解决问题:
- 初始化两个变量,比如X为0和Y为N-1来存储最长等前缀字符串的结束索引和最长等后缀字符串的开始索引。
- 迭代过的字符串B的字符,然后在每次迭代中检查当前的字符在字符串A的索引X,然后通过递增1 X等于字符。否则,断。
- 迭代过的字符串B的反转的字符,然后在每次迭代中检查,如果当前字符是等于在字符串A的指数Y上的字符,然后减1年。否则,断。
- 现在,如果N和M之间的差值等于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 现场工作专业课程和学生竞争性编程现场课程。