Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class WordLadder {
- private String lastWord = "";
- private Lexicom lexicom = new Lexicom("english.dat");
- private boolean running = true;
- public WordLadder() {
- Scanner scanner = new Scanner(System.in);
- while (running) {
- checkWord(scanner.readLine());
- }
- }
- public static void main(String[] args) {
- new WordLadder():
- }
- private void checkWord(String word) {
- if (word.length() == 0) {
- System.out.println();
- running = false;
- return;
- }
- System.out.println(word);
- if ((lastWord.length() != 0 && word.length() != lastWord.length()) || !lexicom.isEnglishWord(word)) {
- System.out.println("Invalid word");
- return;
- }
- int occurrences = 0;
- char[] currWord = word.toCharArray();
- char[] lastWord = this.lastWord.toCharArray();
- for (int i = 0; i < currWord.length; i++) {
- for (int j = 0; j < lastWord.length; j++) {
- if (currWord[i] == lastWord[i]) {
- occurrences++;
- }
- }
- }
- if (occurrences > 1) {
- System.out.println("Invalid word");
- } else {
- lastWord = word;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement