• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anagrams

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Raymond Xu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would i implement the sort() on the Array?
 
Raymond Xu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


java.util.Arrays.sort(xarray);
 
Raymond Xu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!");
}
}

}
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic