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.
Input: str = “toy”, str2 = “try”
The given two words could be obtained from either word “tory” or word “troy”. So output is 2.
Input: str1 = “sweet”, str2 = “sheep”
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:
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- 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
- Count of sub-strings with equal consecutive 0's and 1's
- Count of binary strings of length N having equal count of 0's and 1's
- Count the number of strings in an array whose distinct characters are less than equal to M
- Count of same length Strings that exists lexicographically in between two given Strings
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Meta Strings (Check if two strings can become same after a swap in one string)
- Search in an array of strings where non-empty strings are sorted
- Minimum characters to be deleted from the end to make given two strings equal
- Check if two strings can be made equal by swapping one character among each other
- Number of character corrections in the given strings to make them equal
- Check whether two strings can be made equal by increasing prefixes
- Minimum move to end operations to make all strings equal
- Check if two strings after processing backspace character are equal or not
- Pairs of complete strings in two sets of strings
- Number of common base strings for two strings
- Minimum number of operations required to make two strings equal
- Check whether two strings can be made equal by copying their characters with the adjacent ones
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.