package cse1030.games.wordgames;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* A utility class containing methods related to word games.
*
* @author CSE1030_W14
*
*/
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();
}
/**
* 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
:
*
*
s
is in
* WordGamesUtility.DICTIONARY
t
is in
* WordGamesUtility.DICTIONARY
s
is the same length as t
s
and t
are identical except in exactly one
* positiontrue
if s
and t
are
* adjacent, and false
otherwise
*/
public static boolean areAdjacent(String s, String t) {
// is s in the dictionary?
if (!WordGamesUtil.DICTIONARY.contains(s)) {
return false;
}
// is t in the dictionary?
if (!WordGamesUtil.DICTIONARY.contains(t)) {
return false;
}
// do s and t have the same length?
if (s.length() != t.length()) {
return false;
}
// count the number of letters n that are different in s and t
int n = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
n++;
}
}
return n == 1;
}
/**
* Returns a sorted set of all words in
* WordGamesUtility.DICTIONARY
that are adjacent to the string
* s
. The sorting of the elements of the set is lexicographical
* (dictionary order).
*
* @param s
* a string
* @return a sorted set of words in WordGamesUtility.DICTIONARY
* that are adjacent to s
*/
public static SortedSets
is an isogram. This method returns
* true
if and only if all of the following conditions are
* true
:
*
* s
is in
* WordGamesUtility.DICTIONARY
s
occur only oncetrue
if a
is an isogram of s
* , and false
otherwise
*/
public static boolean isIsogram(String s) {
// is s in the dictionary?
if (!WordGamesUtil.DICTIONARY.contains(s)) {
return false;
}
SetWordGamesUtility.DICTIONARY
. The sorting of the elements of
* the set is lexicographical (dictionary order).
*
* @return a sorted set of isograms in
* WordGamesUtility.DICTIONARY
*/
public static SortedSets
* in the same order that they appear in s
*
* @param s
* a string
* @return a new list containing each character in the string s
* in the same order that they appear in s
*/
public static Listt
in sequential order
*/
public static String fromList(Listtrue
if and only if all of the following conditions
* are true
:
*
* a
is in
* WordGamesUtility.DICTIONARY
s
is in
* WordGamesUtility.DICTIONARY
a
is the same length as s
a
occur with the same frequency as
* the letters in s
true
if a
is an anagram of s
* , and false
otherwise
*/
public static boolean isAnagramOf(String a, String s) {
if (!WordGamesUtil.DICTIONARY.contains(a)) {
return false;
}
if (!WordGamesUtil.DICTIONARY.contains(s)) {
return false;
}
Lists
, and is created by randomly shuffling the order of the
* letters in s
. All possible permutations of the letters of
* s
are expected to occur with approximately equal likelihood.
*
* @param s
* a string
* @return an new string having the same length as s
, and
* containing all of the letters of s
in a new random
* order
*/
public static String scramble(String s) {
List