Created
June 11, 2016 20:01
-
-
Save wowselim/5868b0888e1b1625149ff2886f29703d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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