/** A class representing strings that implements the Lexicographical interface. Here we assume that strings consist only of lower-case letters. The constructor for this class will accept arbitrary strings, replacing all illegal characters with the terminator character. */ public class TrieString implements Lexicographical { // the string being represented private String data; // the number of legal characters, including // the terminator. private static int alphabetSize = 27; /** A constructor taking a string to be represented. @param d the string to be represented */ public TrieString(String d) { data=d; } /** @return the string being represented */ public String toString() { return data; } // implementation of the Lexicographical interface /** @return the length of the string being represented (including any illegal characters). */ public int length() { return data.length(); } /** @return the number of legal characters, including the terminator */ public int getAlphabetSize() { return alphabetSize; } /** @return the character code of the character of the string in the given position (that is, the position of the character in the alphabet's natural ordering, with the terminator character counting as the 0th character. Returns 0 if there is no legal character in the given position. */ public int elementAt(int i) { if (i>=data.length()) return 0; char c=data.charAt(i); if (c>='a' && c<='z') return 1 + (int) c - (int) 'a'; else return 0; } };