Skip to content

Instantly share code, notes, and snippets.

@wowselim
Created June 11, 2016 20:01
Show Gist options
  • Save wowselim/5868b0888e1b1625149ff2886f29703d to your computer and use it in GitHub Desktop.
Save wowselim/5868b0888e1b1625149ff2886f29703d to your computer and use it in GitHub Desktop.
public class ScrabbleWordTester {
public static void main(String[] args) {
char[] chars = { 'a', 'u', 'b', 'm' };
System.out.println(charsCanBuildString(chars, "baum"));
System.out.println(charsCanBuildString(chars, "muab"));
System.out.println(charsCanBuildString(chars, "ab"));
System.out.println(charsCanBuildString(chars, "babo"));
System.out.println(charsCanBuildString(chars, ""));
}
public static boolean charsCanBuildString(final char[] chars, final String word) {
if(chars.length < word.length())
return false;
else {
String copy = new String(word);
for(char c : chars)
copy = copy.replaceFirst(String.valueOf(c), "");
return copy.length() == 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment