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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Given two strings check which string makes a palindrome first
- Check if K palindromic strings can be formed from a given string
- Check if given string can be split into four distinct strings
- Check whether given string can be generated after concatenating given strings
- Check if given string can be formed by two other strings or their permutations
- Check if a string contains two non overlapping sub-strings "geek" and "keeg"
- Check if a string can be split into two strings with same number of K-frequent characters
- Count of same length Strings that exists lexicographically in between two given Strings
- Search in an array of strings where non-empty strings are sorted
- Pairs of complete strings in two sets of strings
- Count of strings that become equal to one of the two strings after one removal
- Number of common base strings for two strings
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Check if strings are rotations of each other or not | Set 2
- Program to check if two strings are same or not
- Check if two strings are k-anagrams or not
- Check whether Strings are k distance apart or not
- Check if two strings are permutation of each other
- Check if two given strings are isomorphic to each other
- Check whether two strings are anagram of each other