public class WordGram {
private int myHash;
public WordGram
(String[] source,
int start,
int size
) { System.
arraycopy(source, start, myWords,
0, size
); myHash = hashCode();
}
public String wordAt
(int index
) { if (index < 0 || index >= myWords.length) {
}
return myWords[index];
}
public int length(){
// TODO: Complete this method
return myWords.length;
}
for (int k =0; k<myWords.length; k++) {
ret += myWords[k] + " " ;
}// TODO: Complete this method
return ret.trim();
}
public boolean equals
(Object o
) { WordGram other = (WordGram) o;
if (length() != other.length()){
return false;
}
for (int k =0; k < myWords.length; k++) {
if(!myWords[k].equals(other.wordAt(k))){
return false;
}
}// TODO: Complete this method
return true;
}
public WordGram shiftAdd
(String word
) { WordGram out = new WordGram(myWords, 0, myWords.length);
for (int k =0; k < myWords.length-1; k++) {
myWords[k]= myWords[k+1];
}
myWords[myWords.length - 1] = word;
// shift all words one towards 0 and add word at the end.
// you lose the first word
// TODO: Complete this method
return out;
}
public int hashCode() {
for (int k = 0; k < myWords.length; k++){
s += myWords[k];
}
//System.out.println ("s: " + s);
return s.hashCode();
}
}
cHVibGljIGNsYXNzIFdvcmRHcmFtIHsKICAgIHByaXZhdGUgU3RyaW5nW10gbXlXb3JkczsKICAgIHByaXZhdGUgaW50IG15SGFzaDsKCiAgICBwdWJsaWMgV29yZEdyYW0oU3RyaW5nW10gc291cmNlLCBpbnQgc3RhcnQsIGludCBzaXplKSB7CiAgICAgICAgbXlXb3JkcyA9IG5ldyBTdHJpbmdbc2l6ZV07CiAgICAgICAgU3lzdGVtLmFycmF5Y29weShzb3VyY2UsIHN0YXJ0LCBteVdvcmRzLCAwLCBzaXplKTsKICAgICAgICBteUhhc2ggPSBoYXNoQ29kZSgpOwogICAgfQoKICAgIHB1YmxpYyBTdHJpbmcgd29yZEF0KGludCBpbmRleCkgewogICAgICAgIGlmIChpbmRleCA8IDAgfHwgaW5kZXggPj0gbXlXb3Jkcy5sZW5ndGgpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IEluZGV4T3V0T2ZCb3VuZHNFeGNlcHRpb24oImJhZCBpbmRleCBpbiB3b3JkQXQgIitpbmRleCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBteVdvcmRzW2luZGV4XTsKICAgIH0KCiAgICBwdWJsaWMgaW50IGxlbmd0aCgpewogICAgICAgIC8vIFRPRE86IENvbXBsZXRlIHRoaXMgbWV0aG9kCiAgICAgICAgcmV0dXJuIG15V29yZHMubGVuZ3RoOwogICAgfQoKICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKXsKICAgICAgICBTdHJpbmcgcmV0ID0gIiI7CiAgICAgICAgZm9yIChpbnQgayA9MDsgazxteVdvcmRzLmxlbmd0aDsgaysrKSB7CiAgICAgICAgICAgIHJldCArPSBteVdvcmRzW2tdICsgIiAiIDsKICAgICAgICB9Ly8gVE9ETzogQ29tcGxldGUgdGhpcyBtZXRob2QKCiAgICAgICAgcmV0dXJuIHJldC50cmltKCk7CiAgICB9CgogICAgcHVibGljIGJvb2xlYW4gZXF1YWxzKE9iamVjdCBvKSB7CiAgICAgICAgV29yZEdyYW0gb3RoZXIgPSAoV29yZEdyYW0pIG87CiAgICAgICAgaWYgKGxlbmd0aCgpICE9IG90aGVyLmxlbmd0aCgpKXsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBrID0wOyBrIDwgbXlXb3Jkcy5sZW5ndGg7IGsrKykgewogICAgICAgICAgICBpZighbXlXb3Jkc1trXS5lcXVhbHMob3RoZXIud29yZEF0KGspKSl7CiAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9Ly8gVE9ETzogQ29tcGxldGUgdGhpcyBtZXRob2QKICAgICAgICByZXR1cm4gdHJ1ZTsKCiAgICB9CgogICAgcHVibGljIFdvcmRHcmFtIHNoaWZ0QWRkKFN0cmluZyB3b3JkKSB7CQogICAgICAgIFdvcmRHcmFtIG91dCA9IG5ldyBXb3JkR3JhbShteVdvcmRzLCAwLCBteVdvcmRzLmxlbmd0aCk7CiAgICAgICAgZm9yIChpbnQgayA9MDsgayA8IG15V29yZHMubGVuZ3RoLTE7IGsrKykgewogICAgICAgICAgICBteVdvcmRzW2tdPSBteVdvcmRzW2srMV07CiAgICAgICAgfQogICAgICAgIG15V29yZHNbbXlXb3Jkcy5sZW5ndGggLSAxXSA9IHdvcmQ7CiAgICAgICAgLy8gc2hpZnQgYWxsIHdvcmRzIG9uZSB0b3dhcmRzIDAgYW5kIGFkZCB3b3JkIGF0IHRoZSBlbmQuIAogICAgICAgLy8geW91IGxvc2UgdGhlIGZpcnN0IHdvcmQKICAgICAgICAvLyBUT0RPOiBDb21wbGV0ZSB0aGlzIG1ldGhvZAogICAgICAgIHJldHVybiBvdXQ7CiAgICB9CiAgICBwdWJsaWMgaW50IGhhc2hDb2RlKCkgewoKICAgICAgICBTdHJpbmcgcyA9ICIiOwogICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgbXlXb3Jkcy5sZW5ndGg7IGsrKyl7CiAgICAgICAgICAgIHMgKz0gbXlXb3Jkc1trXTsKICAgICAgICB9CiAgICAgICAgLy9TeXN0ZW0ub3V0LnByaW50bG4gKCJzOiAiICsgcyk7ICAKICAgICAgICByZXR1cm4gcy5oYXNoQ29kZSgpOwogICAgfQoKfQ==