Check whether two strings are anagram of each other | GeeksforGeeks
Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different.
The above implementation can be further to use only one count array instead of two. We can increment the value in count array for characters in str1 and decrement for characters in str2. Finally, if all count values are 0, then the two strings are anagram of each other.
Method 2 (Count characters)
1) Create count arrays of size 256 for both strings. Initialize all values in count arrays as 0.
2) Iterate through every character of both strings and increment the count of character in the corresponding count arrays.
3) Compare count arrays. If both count arrays are same, then return true.
Method 1 (Use Sorting)
1) Sort both strings
2) Compare the sorted strings
Check Whether Two Strings are Anagram of Each Other
Print all pairs of anagrams in a given array of strings
Read full article from Check whether two strings are anagram of each other | GeeksforGeeks
Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different.
The above implementation can be further to use only one count array instead of two. We can increment the value in count array for characters in str1 and decrement for characters in str2. Finally, if all count values are 0, then the two strings are anagram of each other.
bool
areAnagram(
char
*str1,
char
*str2)
{
// Create two count arrays and initialize all values as 0
int
count[NO_OF_CHARS] = {0};
int
i;
// For each character in input strings, increment count in
// the corresponding count array
for
(i = 0; str1[i] && str2[i]; i++)
{
count[str1[i]]++;
count[str2[i]]--;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if
(str1[i] || str2[i])
return
false
;
// See if there is any non-zero value in count array
for
(i = 0; i < NO_OF_CHARS; i++)
if
(count[i])
return
false
;
return
true
;
}
Method 2 (Count characters)
1) Create count arrays of size 256 for both strings. Initialize all values in count arrays as 0.
2) Iterate through every character of both strings and increment the count of character in the corresponding count arrays.
3) Compare count arrays. If both count arrays are same, then return true.
bool
areAnagram(
char
*str1,
char
*str2)
{
// Create two count arrays and initialize all values as 0
int
count1[NO_OF_CHARS] = {0};
int
count2[NO_OF_CHARS] = {0};
int
i;
// For each character in input strings, increment count in
// the corresponding count array
for
(i = 0; str1[i] && str2[i]; i++)
{
count1[str1[i]]++;
count2[str2[i]]++;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if
(str1[i] || str2[i])
return
false
;
// Compare count arrays
for
(i = 0; i < NO_OF_CHARS; i++)
if
(count1[i] != count2[i])
return
false
;
return
true
;
}
Method 1 (Use Sorting)
1) Sort both strings
2) Compare the sorted strings
Check Whether Two Strings are Anagram of Each Other
void
findAllAnagrams(string arr[],
int
n)
{
for
(
int
i = 0; i < n; i++)
for
(
int
j = i+1; j < n; j++)
if
(areAnagram(arr[i], arr[j]))
cout << arr[i] <<
" is anagram of "
<< arr[j] << endl;
}
Read full article from Check whether two strings are anagram of each other | GeeksforGeeks