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:
- 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
- Search in an array of strings where non-empty strings are sorted
- Meta Strings (Check if two strings can become same after a swap in one string)
- Minimum move to end operations to make all strings equal
- Check whether two strings can be made equal by increasing prefixes
- 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 copying their characters with the adjacent ones
- Minimum number of given operations required to make two strings equal
- Pairs of complete strings in two sets of strings
- Number of common base strings for two strings
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count of sub-strings that contain character X at least once
- Count strings that end with the given pattern
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.