Check if two strings are k-anagrams or not
PROBLEM :
Given two strings of lowercase alphabets and a value k, Your task is to complete the function which returns if two strings are K-anagrams of each other or not.
Two strings are called k-anagrams if following two conditions are true.
1. Both have same number of characters.
2. Two strings can become anagram by changing at most k characters in a string.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Th first line of input contains two Strings Str1 and Str2. And next line contains an integer k, which denotes number of characters can be replaced.
Output:
Print the respective output in the respective line.
Constraints:
1<=T<=100
1<=K<=|length of String|
Example:
Input:
1
fodr gork
2
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the function below*/
bool areKAnagrams(string str1, string str2, int k)
{
if(str1.length()!=str2.length())
return false ;
int map[256]={0} ;
for(int i=0;i<str1.length();i++)
map[str1[i]]++ ;
int count=0 ;
for(int i=0;i<str2.length();i++)
{
if(map[str2[i]]==0)
count++ ;
else
map[str2[i]]-- ;
}
if(count<=k)
return true ;
return false ;
}
---------------------------------------------------------------------------------
Given two strings of lowercase alphabets and a value k, Your task is to complete the function which returns if two strings are K-anagrams of each other or not.
Two strings are called k-anagrams if following two conditions are true.
1. Both have same number of characters.
2. Two strings can become anagram by changing at most k characters in a string.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Th first line of input contains two Strings Str1 and Str2. And next line contains an integer k, which denotes number of characters can be replaced.
Output:
Print the respective output in the respective line.
Constraints:
1<=T<=100
1<=K<=|length of String|
Example:
Input:
1
fodr gork
2
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the function below*/
bool areKAnagrams(string str1, string str2, int k)
{
if(str1.length()!=str2.length())
return false ;
int map[256]={0} ;
for(int i=0;i<str1.length();i++)
map[str1[i]]++ ;
int count=0 ;
for(int i=0;i<str2.length();i++)
{
if(map[str2[i]]==0)
count++ ;
else
map[str2[i]]-- ;
}
if(count<=k)
return true ;
return false ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment