The original word or phrase is known as the subject of the anagram. Any word or phrase that exactly reproduces the letters in another order is an anagram. Someone who creates anagrams may be called an "anagrammatist",[1] and the goal of a serious or skilled anagrammatist is to produce anagrams that in some way reflect or comment on their subject.[source wiki]
Example :
ABC and BCA or ACB or BAC are Anagram
Why?
Because every single letter in the first word, is owned by the second word with the same amount. So if the first word contains the letter A of 1, then the second word must also have the letter A as much as 1. If in the first word contains the letter B of 2, then the second word must also have the letter B as much as 2, and so on
ABC and BBA or ABBC or ABCA or ABA or BAA are not an Anagram
As like explained above example, in this second example, the letter on the first word and the second word are same, but the amount are different. That's make why in the second example is not an Anagram.
How the implementing on Java?
Here I will give you 2 different methods that can used to check two words is an anagram or not an anagram.
Method I
On this first method is to sort the first word and second word from the beginning to the end (alphabetical order a-z).
That used to sort is the Arrays.sort code (chars1). So from the input BCA will turn into ABC. The goal is let program easy to detect how many of each letter if it is in the order.
The result will like the follows :
CCABD and BACD, after the sort will be ABCCD and ABCA, then it is caught if the two words are not an anagram
What determines the amount is the same or not is in the if section ie Arrays.equals (chars1, chars2).
See also : How to Watch Youtube's Video From Own Android Application
Understood?
Method II
On this second method I think is more complex than the first method because it plays more logic than the existing function in java.
The first is to check the number of first words and the second word must be the same, if the difference in number is already certain it is not Anagram.
See also : Android - Get Value Of List Checkbox And Display It On Other Activity As A List
The second checks each letter between the first word and the second word, before determining whether it is an anagram or not
Result not an anagram |
The last one determines the Anagram or not.
In this method can also find out which letters are causing the two words are not Anagram and how many.
What if there are upper and lower case letters?
No problem, if your case requires that the upper and lower case is the same then use the function .toLowerCase ().
If your case requires differentiating between uppercase and lowercase letters, the .toLowerCase () function should not be used.
How to use the method
Result
Result an anagra |
So that's all the tutorials, you can use any one of the method, both can determine the two words Anagram or not.
EmoticonEmoticon