Given two strings str1 and str2, the task is to count the characters in str1 such that after removing any one of them str1 becomes identical to str2. Also, print positions of these characters. If it is not possible then print -1.
Examples:
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
Approach: Find the length of longest common prefix let it be l and the length of the longest common suffix let it be r of two strings. The solution is clearly not possible if
- len(str) != len(str2) + 1
- len(str1) + 1 < n – r
Otherwise, the valid indices are from max(len(str1) – r, 1) to min(l + 1, len(str1))
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Find_Index(string str1, string str2)
{
int n = str1.size();
int m = str2.size();
int l = 0;
int r = 0;
if (n != m + 1) {
return -1;
}
for ( int i = 0; i < m; i++) {
if (str1[i] == str2[i]) {
l += 1;
}
else {
break ;
}
}
int i = n - 1;
int j = m - 1;
while (i >= 0 && j >= 0 && str1[i] == str2[j]) {
r += 1;
i -= 1;
j -= 1;
}
if (l + r < m) {
return -1;
}
else {
i = max(n - r, 1);
j = min(l + 1, n);
return (j - i + 1);
}
}
int main()
{
string str1 = "aaa" , str2 = "aa" ;
cout << Find_Index(str1, str2);
return 0;
}
|
Java
class GFG {
static int Find_Index(String str1, String str2)
{
int n = str1.length();
int m = str2.length();
int l = 0 ;
int r = 0 ;
if (n != m + 1 ) {
return - 1 ;
}
for ( int i = 0 ; i < m; i++) {
if (str1.charAt(i) == str2.charAt(i)) {
l += 1 ;
}
else {
break ;
}
}
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 (l + r < m) {
return - 1 ;
}
else {
i = Math.max(n - r, 1 );
j = Math.min(l + 1 , n);
return (j - i + 1 );
}
}
public static void main(String[] args)
{
String str1 = "aaa" , str2 = "aa" ;
System.out.println(Find_Index(str1, str2));
}
}
|
Python3
def Find_Index(str1, str2):
n = len (str1)
m = len (str2)
l = 0
r = 0
if (n ! = m + 1 ):
return - 1
for i in range (m):
if str1[i] = = str2[i]:
l + = 1
else :
break
i = n - 1
j = m - 1
while i > = 0 and j > = 0 and str1[i] = = str2[j]:
r + = 1
i - = 1
j - = 1
if l + r < m:
return - 1
else :
i = max (n - r, 1 )
j = min (l + 1 , n)
return (j - i + 1 )
if __name__ = = "__main__" :
str1 = "aaa"
str2 = "aa"
print (Find_Index(str1, str2))
|
C#
using System;
class GFG {
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;
if (n != m + 1) {
return -1;
}
for (i = 0; i < m; i++) {
if (str1[i] == str2[i]) {
l += 1;
}
else {
break ;
}
}
i = n - 1;
j = m - 1;
while (i >= 0 && j >= 0 && str1[i] == str2[j]) {
r += 1;
i -= 1;
j -= 1;
}
if (l + r < m) {
return -1;
}
else {
i = Math.Max(n - r, 1);
j = Math.Min(l + 1, n);
return (j - i + 1);
}
}
public static void Main(String[] args)
{
String str1 = "aaa" , str2 = "aa" ;
Console.WriteLine(Find_Index(str1, str2));
}
}
|
Javascript
<script>
function Find_index(str1,str2)
{
var n = str1.length;
var m = str2.length;
var l = 0;
var r = 0;
if (n != m + 1)
{
return -1;
}
for ( var i = 0; i < m; i++)
{
if (str1[i] == str2[i])
{
l += 1;
}
else
{
break ;
}
}
var i = n - 1;
var j = m - 1;
while (i >= 0 && j >= 0 &&
str1[i] == str2[j])
{
r += 1;
i -= 1;
j -= 1;
}
if (l + r < m)
{
return -1;
}
else
{
i = Math.max(n - r, 1);
j = Math.min(l + 1, n);
return (j - i + 1);
}
}
var str1 = "aaa" ;
var str2 = "aa" ;
var result = Find_index(str1,str2);
document.write(result);
</script>
|
Time Complexity: O(n+m) // where n is the length of the first string and m is the length of the second string
Space Complexity: O(1) //no extra space is used
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Jul, 2022
Like Article
Save Article