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

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:

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

 ``

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

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!

Previous
Next