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:
- 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
- Minimum number of given operations required to make two strings equal
- Number of common base strings for two strings
- Pairs of complete strings in two sets of strings
- Count strings that end with the given pattern
- Count of sub-strings that are divisible by K
- Count All Palindrome Sub-Strings in a String | Set 1
- Count of strings that can be formed using a, b and c under given constraints
- Count common subsequence in two strings
- Count All Palindrome Sub-Strings in a String | Set 2
- Count of strings that can be formed from another string using each character at-most once
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.