import java.util.SortedSet; import java.util.TreeSet; /** * A utility class containing methods related to word games. * * @author EECS1030_W15 * */ public class WordGamesUtil { /** * A dictionary of lower case English words. The dictionary cannot be * modified. */ public static final Dictionary DICTIONARY = Dictionary.INSTANCE; /** * Private constructor to prevent instantiation. */ private WordGamesUtil() { throw new UnsupportedOperationException(); } /** * Returns the Hamming distance between two strings. The Hamming * distance is the number of indices where the characters in * two strings are different. * * @param s a string * @param t a string * @return the Hamming distance between s and t * @throws IllegalArgumentException if s.length() != t.length() */ public static int distance(String s, String t) { if (s.length() != t.length()) { throw new IllegalArgumentException("strings have different lengths"); } int n = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != t.charAt(i)) { n++; } } return n; } /** * Determines if two strings are adjacent words. The string s is * adjacent to t if and only if all of the following conditions * are true: * *
For example, suppose that we want to find all words
* that can be formed by changing the 't' in "cat".
* alldjacentTo("cat", 2) returns the set containing the
* strings "cab", "cad", "cam", "can", "cap", "car", "caw", "cay".
*
* @param s
* a string
* @param index the index where the adjacent words differ from s
* @pre. index >= 0 && index < s.length()
* @return a sorted set of words in WordGamesUtility.DICTIONARY
* that are adjacent to s and differ from s exactly
* at the given index
*/
public static SortedSet