Need help creating algorithm

I need to write a class called Scrabble which has a constructor:

public Scrabble(String s1)

and contains a method:

public boolean scrabble(String s2)

and any other methods and variables I need.

Assume that s1 and s2 are made of the 26 lower case ascii characters a....z. If the following code is executed:

(new Scrabble(s1)).scrabble(s2);

the return is true if s2 can be built using characters from s1, and false otherwise. Each occurrence of a character can only be used once. For example,

(new Scrabble("abc")).scrabble("ba");

returns true and

(new Scrabble("bbc")).scrabble("bbb");

returns false.

I have no idea what to do as Im new to java, any advice would be much appreciated.

[767 byte] By [miztasa] at [2007-9-27 23:35:51]
# 1

Try this to see whether it works.

class Scrabble{

String pre, post;

Scrabble(String s1){

this.pre = s1;

}

public boolean scrabble(String s2){

boolean result=false;

this.post = s2;

if(pre.length()<post.length()) {

return result;

}else{

char[] preArr = pre.toCharArray();

char[] postArr = post.toCharArray();

java.util.Arrays.sort(preArr);

java.util.Arrays.sort(postArr);

int j=0;

LABEL: for(j=0;j<postArr.length;j++){

int i=0;

while(i><preArr.length){

if(preArr[i]==postArr[j]) {

preArr[i]='\0';

postArr[j]='\0';

i++;

continue LABEL;

}

i++;

} //end of while

if(i==preArr.length) break LABEL;

}

if(j==postArr.length) result=true;

}

return result;

}

}

>

fsato4a at 2007-7-7 16:03:21 > top of Java-index,Other Topics,Algorithms...
# 2
>I have no idea what to do as Im new to java, any advice would be much appreciated. This really looks like homework to me.
jschella at 2007-7-7 16:03:21 > top of Java-index,Other Topics,Algorithms...
# 3
I was helping a friend out with his assignment.
miztasa at 2007-7-7 16:03:21 > top of Java-index,Other Topics,Algorithms...
# 4

Heres the skeleton of how I'd do it.

class Word

{

int[] count = new int[26];

Word(String word) throws IllegalArgumentException;

boolean contains(Word other);

}

Then if you really want a Word.scrabble(String other) method it would simply return contains(new Word(other))

YATArchivista at 2007-7-7 16:03:21 > top of Java-index,Other Topics,Algorithms...