Open In App

Count of characters in str1 such that after deleting anyone of them str1 becomes str2

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:
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:
 

 

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 
 

  1. len(str) != len(str2) + 1
  2. 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++




// Below is C++ implementation of the approach
#include <bits/stdc++.h>
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




<script>
 
// Javascript program for the above approach
 
// Function to return the count
// of required indices
function Find_index(str1,str2)
{
    var n = str1.length;
    var m = str2.length;
    var l = 0;
    var r = 0;
 
    // Solution doesn't exist
    if (n != m + 1)
    {
        return -1;
    }
 
    // Find the length of the longest
    // common prefix of strings
    for (var i = 0; i < m; i++)
    {
        if (str1[i] == str2[i])
        {
            l += 1;
        }
        else
        {
            break;
        }
    }
     
    // Find the length of the longest
    // common suffix of strings
    var i = n - 1;
    var 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  
 
// Given Strings
var str1 = "aaa";
var str2 = "aa";
  
  
// Function call
var result = Find_index(str1,str2);
  
// Print the answer
document.write(result);
 
// This code is contributed by rj13to
</script>


Output: 

3

 

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



Last Updated : 28 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads