Class StateSpeller
- java.lang.Object
-
- StateSpeller
-
public class StateSpeller extends java.lang.Object
This class contains methods inspired by the following puzzle, presented by Will Shortz on NPR's weekend edition on February 19th, 2012.The word marten, as in the animal, consists of the beginning letters of Mississippi, Arkansas, Texas, and New Mexico. And you can actually drive from Mississippi to Arkansas to Texas to New Mexico, in that order. What is the longest common English word you can spell by taking the beginning letters of consecutive states in order, as you travel through them? My answer has eight letters. Maybe you can better mine. The longest answer will win.
- Version:
- ??
- Author:
- CS 159 Instructors and ??
-
-
Constructor Summary
Constructors Constructor Description StateSpeller(java.lang.String stateFile)
The constructor sets up the state graph.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.ArrayList<java.lang.String>
howToSpell(java.lang.String word)
Return an ArrayList containing the names of states than can be used to spell the provided word using any number of letters taken from the beginnings of the names of visited states.java.util.ArrayList<java.lang.String>
howToSpellFirstLetters(java.lang.String word)
Returns an ArrayList containing the names of states than can be used to spell the provided word using the first letters of visited states.boolean
isSpellableFirstLetters(java.lang.String word)
Returns true if the provided word can be spelled by starting in any state and using only the first letters of state names.
-
-
-
Constructor Detail
-
StateSpeller
public StateSpeller(java.lang.String stateFile) throws java.io.FileNotFoundException
The constructor sets up the state graph.- Parameters:
stateFile
- A file containing the state adjacency information- Throws:
java.io.FileNotFoundException
- If the file cannot be opened for reading
-
-
Method Detail
-
isSpellableFirstLetters
public boolean isSpellableFirstLetters(java.lang.String word)
Returns true if the provided word can be spelled by starting in any state and using only the first letters of state names. Repeated states are allowed. For example, "mom" can be spelled by moving from Michigan to Ohio, then back to Michigan.- Parameters:
word
- The word to spell- Returns:
- true if it can be spelled, false otherwise
-
howToSpellFirstLetters
public java.util.ArrayList<java.lang.String> howToSpellFirstLetters(java.lang.String word)
Returns an ArrayList containing the names of states than can be used to spell the provided word using the first letters of visited states. Returns null if the word cannot be spelled by moving between states. Repeated states are NOT allowed. While there may be multiple ways of spelling the word, this method will return only one. For example, the return value for "amok" is ["Arkansas", "Missouri", "Oklahoma", "Kansas"].- Parameters:
word
- The word to spell- Returns:
- ArrayList of state names used to spell the word, or null if the word cannot be spelled
-
howToSpell
public java.util.ArrayList<java.lang.String> howToSpell(java.lang.String word)
Return an ArrayList containing the names of states than can be used to spell the provided word using any number of letters taken from the beginnings of the names of visited states. Return null if the word cannot be spelled by moving between states. Repeated states are NOT allowed. While there may be multiple ways of spelling the word, this method will return only one. For example, the return value for "omission" is ["Oklahoma", "Missouri", "Iowa", "Nebraska"].- Parameters:
word
- The word to spell- Returns:
- ArrayList of state names used to spell the word, or null if the word cannot be spelled
-
-