Given two strings str1 and str2, the task is to count all the valid strings. An example of a valid string is given below:
If str1 = “toy” and str2 = “try”. Then S = “tory” is a valid string because when a single character is removed from it i.e. S = “tory” = “try” it becomes equal to str1. This property must also be valid with str2 i.e. S = “tory” = “toy” = str2.
The task is to print the count of all possible valid strings.
Examples:
Input: str = “toy”, str2 = “try”
Output: 2
The given two words could be obtained from either word “tory” or word “troy”. So output is 2.Input: str1 = “sweet”, str2 = “sheep”
Output: 0
The two given word couldn’t be obtained from the same word by removing one letter.
Approach: Calculate A as a longest common prefix of str1 and str2 and C as a longest common suffix of str1 and str2. If both the string are equal then 26 * (n + 1) strings are possible. Otherwise, set count = 0 and l equal to the first index in that is not a part of the common prefix and r is the rightmost index which is not a part of the common suffix.
Now, if str1[l+1 … r] = str2[l … r-1] then update count = count + 1.
And if str1[l … r-1] = str2[l+1 … r] then update count = count + 1.
Print the count in the end.
Below is the implementation of the approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count of the // required strings int findAnswer(string str1, string str2, int n) { int l, r; int ans = 2; // Searching index after longest common // prefix ends for (int i = 0; i < n; ++i) { if (str1[i] != str2[i]) { l = i; break; } } // Searching index before longest common // suffix ends for (int i = n - 1; i >= 0; i--) { if (str1[i] != str2[i]) { r = i; break; } } // If str1 = str2 if (r < l) return 26 * (n + 1); // If only 1 character is different // in both the strings else if (l == r) return ans; else { // Checking remaining part of string // for equality for (int i = l + 1; i <= r; i++) { if (str1[i] != str2[i - 1]) { ans--; break; } } // Searching in right of string h // (g to h) for (int i = l + 1; i <= r; i++) { if (str1[i - 1] != str2[i]) { ans--; break; } } return ans; } } // Driver code int main() { string str1 = "toy", str2 = "try"; int n = str1.length(); cout << findAnswer(str1, str2, n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the count of the // required strings static int findAnswer(String str1, String str2, int n) { int l = 0, r = 0; int ans = 2; // Searching index after longest common // prefix ends for (int i = 0; i < n; ++i) { if (str1.charAt(i) != str2.charAt(i)) { l = i; break; } } // Searching index before longest common // suffix ends for (int i = n - 1; i >= 0; i--) { if (str1.charAt(i) != str2.charAt(i)) { r = i; break; } } // If str1 = str2 if (r < l) return 26 * (n + 1); // If only 1 character is different // in both the strings else if (l == r) return ans; else { // Checking remaining part of string // for equality for (int i = l + 1; i <= r; i++) { if (str1.charAt(i) != str2.charAt(i - 1)) { ans--; break; } } // Searching in right of string h // (g to h) for (int i = l + 1; i <= r; i++) { if (str1.charAt(i-1) != str2.charAt(i)) { ans--; break; } } return ans; } } // Driver code public static void main(String args[]) { String str1 = "toy", str2 = "try"; int n = str1.length(); System.out.println(findAnswer(str1, str2, n)); } } // This code is contributed by // Surendra_Gangwar |
Python3
# Python3 implementation of the approach import math as mt # Function to return the count of # the required strings def findAnswer(str1, str2, n): l, r = 0, 0 ans = 2 # Searching index after longest # common prefix ends for i in range(n): if (str1[i] != str2[i]): l = i break # Searching index before longest # common suffix ends for i in range(n - 1, -1, -1): if (str1[i] != str2[i]): r = i break if (r < l): return 26 * (n + 1) # If only 1 character is different # in both the strings elif (l == r): return ans else: # Checking remaining part of # string for equality for i in range(l + 1, r + 1): if (str1[i] != str2[i - 1]): ans -= 1 break # Searching in right of string h # (g to h) for i in range(l + 1, r + 1): if (str1[i - 1] != str2[i]): ans -= 1 break return ans # Driver code str1 = "toy"str2 = "try"n = len(str1) print(findAnswer(str1, str2, n)) # This code is contributed # by Mohit kumar 29 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the count of the // required strings static int findAnswer(string str1, string str2, int n) { int l = 0, r = 0; int ans = 2; // Searching index after longest common // prefix ends for (int i = 0; i < n; ++i) { if (str1[i] != str2[i]) { l = i; break; } } // Searching index before longest common // suffix ends for (int i = n - 1; i >= 0; i--) { if (str1[i] != str2[i]) { r = i; break; } } // If str1 = str2 if (r < l) return 26 * (n + 1); // If only 1 character is different // in both the strings else if (l == r) return ans; else { // Checking remaining part of string // for equality for (int i = l + 1; i <= r; i++) { if (str1[i] != str2[i - 1]) { ans--; break; } } // Searching in right of string h // (g to h) for (int i = l + 1; i <= r; i++) { if (str1[i-1] != str2[i]) { ans--; break; } } return ans; } } // Driver code public static void Main() { String str1 = "toy", str2 = "try"; int n = str1.Length; Console.WriteLine(findAnswer(str1, str2, n)); } } // This code is contributed by // shs |
PHP
<?php // PHP implementation of the above approach // Function to return the count of // the required strings function findAnswer($str1, $str2, $n) { $ans = 2; // Searching index after longest // common prefix ends for ($i = 0; $i < $n; ++$i) { if ($str1[$i] != $str2[$i]) { $l = $i; break; } } // Searching index before longest // common suffix ends for ($i = $n - 1; $i >= 0; $i--) { if ($str1[$i] != $str2[$i]) { $r = $i; break; } } // If str1 = str2 if ($r < $l) return 26 * ($n + 1); // If only 1 character is different // in both the strings else if ($l == $r) return $ans; else { // Checking remaining part of string // for equality for ($i = $l + 1; $i <= $r; $i++) { if ($str1[$i] != $str2[$i - 1]) { $ans--; break; } } // Searching in right of string h // (g to h) for ($i = $l + 1; $i <= $r; $i++) { if ($str1[$i - 1] != $str2[$i]) { $ans--; break; } } return $ans; } } // Driver code $str1 = "toy"; $str2 = "try"; $n = strlen($str1); echo findAnswer($str1, $str2, $n); // This code is contributed by Ryuga ?> |
2
Recommended Posts:
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if frequency of all characters can become same by one removal
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Minimize the maximum minimum difference after one removal from array
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Remove minimum number of characters so that two strings become anagram
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Maximum length of balanced string after swapping and removal of characters
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Lexicographically largest possible String after removal of K characters
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Print the Forests of a Binary Tree after removal of given Nodes
- Longest palindromic string possible after removal of a substring
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- How can one become good at Data structures and Algorithms easily?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


