File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Anagrams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Anagrams" Watch "Anagrams" New topic
Author

Anagrams

Raymond Xu
Greenhorn

Joined: Nov 11, 2007
Posts: 8
I need help in implementing an algorithm in java for an Anagram test.
Ex:
to be / bo te (yes)
foo / fo (no)

A friend recommend this algorithm:
The idea is to convert the strings to arrays. When a loop encounters a letter in the first string then it looks for one in the second. If it finds it, it blanks it out. If the second array does not contain letters in the end, then it's an anagram.

I'm trying to write a method within a class to do the test. At that point the strings would have been converted to two separate arrays. Any simple ideas or hints for me?? I'm having trouble figuring out how to march down the letters in the first array and blanking letters out in the second array.
Raymond Xu
Greenhorn

Joined: Nov 11, 2007
Posts: 8
Actually, I'm going to take a simplistic approach.

ublic class Anagram{

private String x;
private String y;

public Anagram(String phrase1, String phrase2){
x = phrase1;
y = phrase2;
}

public void lettersort(){
toLowerCase();
toCharArray();
sort();
}

public void testAnagram(){
String[] x1 = x.lettersort();
String[] x2 = y.lettersort();
if(x1 == x2){
System.out.println(yes);
}
else{
System.out.println(no);
}
}

}

Any troubleshooting comments??
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
How about using Ascii total of both the strings to validate ?

For your Anagram solution I can think of using two checks:
1. Check the string length of both the strings. If different return false
else
2. get the total ascii value of all the chars in string1 and compare with that of second string2.
You can use toCharArray() method of String class to get the char array, iterate this and add all the chars which gives the total ascii value.


You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Originally posted by Rajah Nagur:
How about using Ascii total of both the strings to validate ?

That would make "AC" equal to "BB", since they both have the same Ascii total of 132.

As for the length check, that would be a useful first check, as it removes the need for sorting the string, which may be time consuming if either string is large.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raymond Xu
Greenhorn

Joined: Nov 11, 2007
Posts: 8
How would i implement the sort() on the Array?
Raymond Xu
Greenhorn

Joined: Nov 11, 2007
Posts: 8
I'm having trouble with sorting the array and defining xarray and yarray.

public class Anagram1{

private String x;
private String y;
private char[] xarray;
private char[] yarray;

public Anagram1(String phrase1, String phrase2){
x = phrase1;
y = phrase2;
}

public void xlettersort(){
x= x.toLowerCase();
xarray = x.toCharArray();
xarray.sort();
}

public void ylettersort(){
y= y.toLowerCase();
yarray = y.toCharArray();
yarray.sort();
}

public void testAnagram(){
xlettersort();
ylettersort();
if(xarray == yarray){
System.out.println("Yes the two phrases are an anagram");
}
else{
System.out.println("No there is no anagram!");
}
}

}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Originally posted by Raymond Xu:
How would i implement the sort() on the Array?

java.util.Arrays.sort(xarray);
Raymond Xu
Greenhorn

Joined: Nov 11, 2007
Posts: 8
Thank you for the help.

Can anyone point out a logical error in this??

public class Anagram1{

private String x;
private String y;
private char xarray[];
private char yarray[];
private String a;
private String b;

public Anagram1(String phrase1, String phrase2){
x = phrase1;
y = phrase2;
}

public void xlettersort(){
x= x.toLowerCase();
xarray = x.toCharArray();
java.util.Arrays.sort(xarray);
a = java.util.Arrays.toString(xarray);

}

public void ylettersort(){
y= y.toLowerCase();
yarray = y.toCharArray();
java.util.Arrays.sort(yarray);
b = java.util.Arrays.toString(yarray);

}

public void testAnagram(){
xlettersort();
ylettersort();
if(a == b){
System.out.println("Yes");
}
else{
System.out.println("No!");
}
}

}
Raymond Xu
Greenhorn

Joined: Nov 11, 2007
Posts: 8
Thank you for the help.

Can anyone point out a logical error in this??

public class Anagram1{

private String x;
private String y;
private char xarray[];
private char yarray[];
private String a;
private String b;

public Anagram1(String phrase1, String phrase2){
x = phrase1;
y = phrase2;
}

public void xlettersort(){
x= x.toLowerCase();
xarray = x.toCharArray();
java.util.Arrays.sort(xarray);
a = java.util.Arrays.toString(xarray);

}

public void ylettersort(){
y= y.toLowerCase();
yarray = y.toCharArray();
java.util.Arrays.sort(yarray);
b = java.util.Arrays.toString(yarray);

}

public void testAnagram(){
xlettersort();
ylettersort();
if(a == b){
System.out.println("Yes");
}
else{
System.out.println("No!");
}
}

}
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3477
    
  13
You're using == to compare strings. This will only be true if the two references point to the same string object. To check if two different string objects have the same content use String.equals() method.


Joanne
 
 
subject: Anagrams