str1 中的字符数,以便在删除其中任何一个后 str1 变为 str2
给定两个字符串str1和str2 ,任务是计算str1中的字符,以便在删除其中任何一个之后str1变得与str2相同。此外,打印这些字符的位置。如果不可能,则打印-1 。
例子:
Input: str1 = “abdrakadabra”, str2 = “abrakadabra”
Output: 1
The only valid character is at index 2 i.e. str1[2]
Input: str1 = “aa”, str2 = “a”
Output: 2
Input: str1 = “geeksforgeeks”, str2 = “competitions”
Output: 0
方法:求两个字符串的最长公共前缀长度为l,最长公共后缀长度为r。解决方案显然是不可能的,如果
- len(str) != len(str2) + 1
- len(str1) + 1 < n – r
否则,有效索引是从 max(len(str1) – r, 1) 到 min(l + 1, len(str1))
下面是上述方法的实现:
C++
// Below is C++ implementation of the approach
#include
using namespace std;
// Function to return the count
// of required indices
int Find_Index(string str1, string str2)
{
int n = str1.size();
int m = str2.size();
int l = 0;
int r = 0;
// Solution doesn't exist
if (n != m + 1) {
return -1;
}
// Find the length of the longest
// common prefix of strings
for (int i = 0; i < m; i++) {
if (str1[i] == str2[i]) {
l += 1;
}
else {
break;
}
}
// Find the length of the longest
// common suffix of strings
int i = n - 1;
int j = m - 1;
while (i >= 0 && j >= 0 && str1[i] == str2[j]) {
r += 1;
i -= 1;
j -= 1;
}
// If solution does not exist
if (l + r < m) {
return -1;
}
// Return the count of indices
else {
i = max(n - r, 1);
j = min(l + 1, n);
return (j - i + 1);
}
}
// Driver code
int main()
{
string str1 = "aaa", str2 = "aa";
cout << Find_Index(str1, str2);
return 0;
}
// This code is contributed by PrinciRaj1992
Java
// Java implementation of the approach
class GFG {
// Function to return the count
// of required indices
static int Find_Index(String str1, String str2)
{
int n = str1.length();
int m = str2.length();
int l = 0;
int r = 0;
// Solution doesn't exist
if (n != m + 1) {
return -1;
}
// Find the length of the longest
// common prefix of strings
for (int i = 0; i < m; i++) {
if (str1.charAt(i) == str2.charAt(i)) {
l += 1;
}
else {
break;
}
}
// Find the length of the longest
// common suffix of strings
int i = n - 1;
int j = m - 1;
while (i >= 0 && j >= 0
&& str1.charAt(i) == str2.charAt(j)) {
r += 1;
i -= 1;
j -= 1;
}
// If solution does not exist
if (l + r < m) {
return -1;
}
// Return the count of indices
else {
i = Math.max(n - r, 1);
j = Math.min(l + 1, n);
return (j - i + 1);
}
}
// Driver code
public static void main(String[] args)
{
String str1 = "aaa", str2 = "aa";
System.out.println(Find_Index(str1, str2));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of the approach
# Function to return the count of required indices
def Find_Index(str1, str2):
n = len(str1)
m = len(str2)
l = 0
r = 0
# Solution doesn't exist
if(n != m + 1):
return -1
# Find the length of the longest
# common prefix of strings
for i in range(m):
if str1[i] == str2[i]:
l += 1
else:
break
# Find the length of the longest
# common suffix of strings
i = n-1
j = m-1
while i >= 0 and j >= 0 and str1[i] == str2[j]:
r += 1
i -= 1
j -= 1
# If solution does not exist
if l + r < m:
return -1
# Return the count of indices
else:
i = max(n-r, 1)
j = min(l + 1, n)
return (j-i + 1)
# Driver code
if __name__ == "__main__":
str1 = "aaa"
str2 = "aa"
print(Find_Index(str1, str2))
C#
// Program to print the given pattern
using System;
class GFG {
// Function to return the count
// of required indices
static int Find_Index(String str1, String str2)
{
int n = str1.Length;
int m = str2.Length;
int l = 0;
int r = 0;
int i, j;
// Solution doesn't exist
if (n != m + 1) {
return -1;
}
// Find the length of the longest
// common prefix of strings
for (i = 0; i < m; i++) {
if (str1[i] == str2[i]) {
l += 1;
}
else {
break;
}
}
// Find the length of the longest
// common suffix of strings
i = n - 1;
j = m - 1;
while (i >= 0 && j >= 0 && str1[i] == str2[j]) {
r += 1;
i -= 1;
j -= 1;
}
// If solution does not exist
if (l + r < m) {
return -1;
}
// Return the count of indices
else {
i = Math.Max(n - r, 1);
j = Math.Min(l + 1, n);
return (j - i + 1);
}
}
// Driver code
public static void Main(String[] args)
{
String str1 = "aaa", str2 = "aa";
Console.WriteLine(Find_Index(str1, str2));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
3