Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are not considered here as Meta strings.
Input : str1 = "geeks" str2 = "keegs" Output : Yes By just swapping 'k' and 'g' in any of string, both will become same. Input : str1 = "rsting" str2 = "string Output : No Input : str1 = "Converse" str2 = "Conserve"
Asked in : Google
Below are steps used in the algorithm.
- Check if both strings are of equal length or not, if not return false.
- Otherwise, start comparing both strings and count number of unmatched characters and also store the index of unmatched characters.
- If unmatched characters are more than 2 then return false.
- Otherwise check if on swapping any of these two characters in any string would make the string equal or not.
- If yes then return true. Otherwise return false.
This article is contributed by Sahil Chhabra (akku). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of strings that become equal to one of the two strings after one removal
- Check if frequency of all characters can become same by one removal
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Check if a string can become empty by recursively deleting a given sub-string
- String slicing in Python to check if a string can become empty by recursive deletion
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Check if a string can be split into two strings with same number of K-frequent characters
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange a string so that all same characters become d distance away
- Check whether two strings contain same characters in same order
- Check if two strings can be made equal by swapping one character among each other
- Remove minimum number of characters so that two strings become anagram
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check whether given string can be generated after concatenating given strings
- Length of longest consecutive ones by at most one swap in a Binary String
- Lexicographically largest string possible in one swap
- Minimum cost to convert one given string to another using swap, insert or delete operations
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Check if all strings of an array can be made same by interchanging characters