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:
- 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 email@example.com. 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.