Coders Packet

Anagrams and its all possibility in C++

By Sakshi Maurya

It will show whether the entered strings are anagram or not and shows all the possibilities of arrangement of strings using C++.


An anagram of a string is another string that contains the same characters set, only the order of characters can be different.

For example, "CAT" and "ACT". Here, the first string "CAT" has three characters 'C', 'A', 'T', and in the second string "ACT" has also three characters 'A', 'C', 'T'.As you can see that both the strings have the same characters only their arrangements are different. Thus these two strings are anagrams.

Similarly, if we take "TAB" and "TAP". Here the character set for the first string is 'T', 'A', 'B', and the character set for the second string is 'T', 'A', 'P'. Thus, both strings have a different character set so these are not anagrams.


1. For finding whether the string is an anagram or not

Firstly, we have compared the length of both strings. If they mismatched, it will print "Characters count mismatched".Then, comparing each element of one string to another (Kind of sorting then comparing). If each character is matched then it will print "An anagram" if not it will print "Not an Anagram".

2. For finding all arrangements with duplicates allowed of the strings

NOTE: If the characters are not anagram or characters count are mismatched, still it will show the all possible arrangements of both the string.

A function "possibility" to print permutations of a string using backtracking. This function takes three arguments:

(I) String (II) Starting index of the string (III) Ending index of the string.


Download Complete Code


No comments yet