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.
Examples:
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.
// C++ program to check if two strings are meta strings #include<iostream> using namespace std;
// Returns true if str1 and str2 are meta strings bool areMetaStrings(string str1, string str2)
{ int len1 = str1.length();
int len2 = str2.length();
// Return false if both are not of equal length
if (len1 != len2)
return false ;
// To store indexes of previously mismatched
// characters
int prev = -1, curr = -1;
int count = 0;
for ( int i=0; i<len1; i++)
{
// If current character doesn't match
if (str1[i] != str2[i])
{
// Count number of unmatched character
count++;
// If unmatched are greater than 2,
// then return false
if (count > 2)
return false ;
// Store both unmatched characters of
// both strings
prev = curr;
curr = i;
}
}
// Check if previous unmatched of string1
// is equal to curr unmatched of string2
// and also check for curr unmatched character,
// if both are same, then return true
return (count == 2 &&
str1[prev] == str2[curr] &&
str1[curr] == str2[prev]);
} // Driver code int main()
{ string str1 = "converse" ;
string str2 = "conserve" ;
areMetaStrings(str1,str2) ? cout << "Yes"
: cout << "No" ;
return 0;
} |
// Java program to check if two strings are meta strings class Test { // Returns true if str1 and str2 are meta strings
static boolean areMetaStrings(String str1, String str2)
{
int len1 = str1.length();
int len2 = str2.length();
// Return false if both are not of equal length
if (len1 != len2)
return false;
// To store indexes of previously mismatched
// characters
int prev = -1, curr = -1;
int count = 0;
for (int i=0; i<len1; i++)
{
// If current character doesn't match
if (str1.charAt(i) != str2.charAt(i))
{
// Count number of unmatched character
count++;
// If unmatched are greater than 2,
// then return false
if (count > 2)
return false;
// Store both unmatched characters of
// both strings
prev = curr;
curr = i;
}
}
// Check if previous unmatched of string1
// is equal to curr unmatched of string2
// and also check for curr unmatched character,
// if both are same, then return true
return (count == 2 &&
str1.charAt(prev) == str2.charAt(curr) &&
str1.charAt(curr) == str2.charAt(prev));
}
// Driver method
public static void main(String args[])
{
String str1 = "converse";
String str2 = "conserve";
System.out.println(areMetaStrings(str1,str2) ? "Yes" :"No");
}
} |
# Python program to check if two strings # are meta strings # Returns true if str1 and str2 are meta strings def areMetaStrings( str1, str2) :
len1 = len (str1)
len2 = len (str2)
# Return false if both are not of equal length
if (len1 ! = len2) :
return False
# To store indexes of previously mismatched
# characters
prev = - 1
curr = - 1
count = 0 i = 0
while i < len1 :
# If current character doesn't match
if (str1[i] ! = str2[i] ) :
# Count number of unmatched character
count = count + 1
# If unmatched are greater than 2,
# then return false
if (count > 2 ) :
return False
# Store both unmatched characters of
# both strings
prev = curr
curr = i
i = i + 1
# Check if previous unmatched of string1
# is equal to curr unmatched of string2
# and also check for curr unmatched character,
# if both are same, then return true
return (count = = 2 and str1[prev] = = str2[curr]
and str1[curr] = = str2[prev])
# Driver method str1 = "converse"
str2 = "conserve"
if ( areMetaStrings(str1,str2) ) :
print "Yes" else :
print "No"
# This code is contributed by Nikita Tiwari. |
// C# program to check if two strings // are meta strings using System;
class GFG {
// Returns true if str1 and str2
// are meta strings
static bool areMetaStrings(String str1,
String str2)
{
int len1 = str1.Length;
int len2 = str2.Length;
// Return false if both are not of
// equal length
if (len1 != len2)
return false ;
// To store indexes of previously
// mismatched characters
int prev = -1, curr = -1;
int count = 0;
for ( int i = 0; i < len1; i++)
{
// If current character
// doesn't match
if (str1[i] != str2[i])
{
// Count number of unmatched
// character
count++;
// If unmatched are greater
// than 2, then return false
if (count > 2)
return false ;
// Store both unmatched
// characters of both strings
prev = curr;
curr = i;
}
}
// Check if previous unmatched of
// string1 is equal to curr unmatched
// of string2 and also check for curr
// unmatched character, if both are
// same, then return true
return (count == 2 &&
str1[prev] == str2[curr] &&
str1[curr] == str2[prev]);
}
// Driver method
public static void Main()
{
String str1 = "converse" ;
String str2 = "conserve" ;
Console.WriteLine(
areMetaStrings(str1,str2)
? "Yes" : "No" );
}
} // This code is contributed by Sam007. |
<?php // php program to check if two strings // are meta strings // Returns true if str1 and str2 are // meta strings function areMetaStrings( $str1 , $str2 )
{ $len1 = strlen ( $str1 );
$len2 = strlen ( $str1 );
// Return false if both are not
// of equal length
if ( $len1 != $len2 )
return false;
// To store indexes of previously
// mismatched characters
$prev = -1; $curr = -1;
$count = 0;
for ( $i = 0; $i < $len1 ; $i ++)
{
// If current character
// doesn't match
if ( $str1 [ $i ] != $str2 [ $i ])
{
// Count number of unmatched
// character
$count ++;
// If unmatched are greater
// than 2, then return false
if ( $count > 2)
return false;
// Store both unmatched
// characters of both
// strings
$prev = $curr ;
$curr = $i ;
}
}
// Check if previous unmatched of
// string1 is equal to curr unmatched
// of string2 and also check for curr
// unmatched character, if both are
// same, then return true
return ( $count == 2 &&
$str1 [ $prev ] == $str2 [ $curr ] &&
$str1 [ $curr ] == $str2 [ $prev ]);
} // Driver code $str1 = "converse" ;
$str2 = "conserve" ;
if (areMetaStrings( $str1 , $str2 ))
echo "Yes" ;
else
echo "No" ;
// This code is contributed by nitin mittal. ?> |
Output:
Yes
Reference :
https://www.careercup.com/question?id=6247626241474560
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 contribute@geeksforgeeks.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.