Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
4

How do you find two strings are anagrams of each other. 

Also how do you find a String is in dictionary or not.

Rishi Yadav
08/19/2011 at 12:27
2 Answers
1
1

public class AnagramTest {

 

private String x

private String y;

privatechar[] xarray;

privatechar[] yarray;

 

public AnagramTest(String phrase1, String phrase2){ 

x = phrase1;

y = phrase2;

}

 

public void xlettersort(){

x= x.toLowerCase();

x=x.replaceAll(" ","");

xarray = x.toCharArray();

java.util.Arrays.sort(xarray);

System.out.println(xarray);

}

 

public void ylettersort(){

 

y= y.toLowerCase();

y=y.replaceAll(" ", "");

System.out.println(y);

yarray = y.toCharArray();

java.util.Arrays.sort(yarray);

System.out.println(yarray);

}

 

public boolean testAnagram(){ 

xlettersort();

ylettersort();

if(xarray.length != yarray.length)

returnfalse;

for(int i=0;i<xarray.length;i++)

{

if(xarray[i]!=yarray[i])

returnfalse;

}

returntrue;

}

}

U N
08/19/2011 at 20:21

2
0

there are a couple of ways: 

*be sure to check the length of the strings if it is different then cancel instantly.

 

1) reverse the second string and compare the two strings.

2) compare by each character. get the first character from the first string and compare it with the last character on the second string do this until you compare all characters. increment for the first and decrement for the second.

Kokok Tolentino
08/19/2011 at 14:36

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details