I am sure this question has been discussed in the distant past several times on Stack Overflow. I am just trying to verify whether my answer is valid or not. I saw this question in this thread. I am sorry that this post is a duplicate of the thread and if it has to be removed, I shall do it.
I thought of doing this in a much more simple way. By just XORing the characters in the string.
So O(n) for XORing each character and O(1) for comparison of last characters in both the strings which gives a O(n) solution.
Even though the last characters may be any special symbol but if the strings are anagrams they still end up being the same. Am I right in this logic?
So instead of doing all sorting and hashing can this solution be adopted? My code goes like this:
char a[7] = "Length";
char b[7] = "enghtL";
for (int i = 1; i < 6; i++) {
a[i] = a[i] ^ a[i-1];
b[i] = b[i] ^ b[i-1];
}
if (a[5] == b[5]) {
cout << "\n The strings are anagrams";
}
else {
cout << "\n No they are not";
}