Posted: 10 Jan, 2021
You are given two strings 'STR1' and 'STR2'. You need to tell whether the strings are meta strings or not.
Meta strings are strings that can be made equal by swapping exactly one pair of distinct characters in one of the strings.
Equal strings are not considered as meta strings.
The first line of input contains an integer 'T' denoting the number of test cases or queries to be run. The first line of each test case or query contains the first string “STR1”. The second line of the test case or query contains the second string “STR2”.
For each test case, print a single line containing “YES” if the strings are meta strings otherwise, “NO”. The output of each test case will be printed in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 10 2 <= length of STR1<= 10 ^ 5 2 <= length of STR2 <= 10 ^ 5
- Check if the two strings are of equal length or not. If not, then it is impossible to make them equal.
- Now, the idea is to count the number of unmatched characters in the strings.
- If the count of unmatched characters is not equal to 2, then it is impossible to make the strings equal. As we have to swap exactly one pair of characters.
- If the count is 2, find their indices and swap the character at these indices.
- After swapping the characters check if the characters at unmatched indices became equal or not.
Palindromes And Indexes
Posted: 7 Jul, 2021
Ninja's Frustrating Homework
Posted: 8 Jul, 2021
Longest Common Prefix
Posted: 24 Jul, 2021
Posted: 29 Jul, 2021
Posted: 21 Aug, 2021