Wednesday, August 16, 2017

Java Determining words An Anagram Or Not

An anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example, the word anagram can be rearranged into "nag a ram".

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


public static void checkAnagram(String str1, String str2) {
char[] chars1 = str1.toLowerCase().toCharArray();
char[] chars2 = str2.toLowerCase().toCharArray();
Arrays.sort(chars1);
Arrays.sort(chars2);
if (Arrays.equals(chars1, chars2)) {
System.out.println(str1 + " dan " + str2 + " adalah Anagram");
} else {
System.out.println(str1 + " dan " + str2 + " bukan merupakan Anagram");
}
}
view raw Anagram.java hosted with ❤ by GitHub
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



private static boolean compareAnagram(String a, String b){
char[] aArr = a.toLowerCase().toCharArray();
char[] bArr = b.toLowerCase().toCharArray();
if (aArr.length != bArr.length){
System.out.println("Jumlah kata tidak sama, silakan masukan jumlah kata yang sama");
return false;
}else{
int[] counts = new int[26]; // An array to hold the number of occurrences of each character
for (int i = 0; i < aArr.length; i++){
counts[aArr[i]-97]++; // Increment the count of the character at i
counts[bArr[i]-97]--; // Decrement the count of the character at i
}
// If the strings are anagrams, the counts array will be full of zeros
for (int i = 0; i<26; i++){
if (counts[i] != 0){
System.out.println(a + " dan " + b + " bukan anagram" + "\nJumlah huruf yang bukan Anagram = " + counts[i]);
System.out.println("Huruf dikata pertama = "+aArr[i] + "\nHuruf dikata kedua = " + bArr[i]);
return false;
}
}
System.out.println(a + " dan " + b + " adalah anagram");
return true;
}
}
view raw Anagram2.java hosted with ❤ by GitHub
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


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pembelajaran;
import java.util.Arrays;
/**
*
* @author putuguna
*/
public class MencariHurufBerbeda {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "aba";
System.out.println("=========METHOD ANAGRAM I=========");
checkAnagram(str1, str2);
System.out.println("========METHOD ANAGRAM II=========");
compareAnagram(str1, str2);
}
public static void checkAnagram(String str1, String str2){
//ketik kodenya disini
}
public static void compareAnagram(String str1, String str2){
//ketik kodenya disini
}
}
view raw Main.java hosted with ❤ by GitHub

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.

Related Posts


EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)