Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class WordLadder {
  5.  
  6.     private String lastWord = "";
  7.  
  8.     private Lexicom lexicom = new Lexicom("english.dat");
  9.  
  10.     public WordLadder() {
  11.         Scanner scanner = new Scanner(System.in);
  12.         while (true) {
  13.             checkWord(scanner.readLine());
  14.         }
  15.     }
  16.  
  17.     public static void main(String[] args) {
  18.         new WordLadder():  
  19.     }
  20.  
  21.     private void checkWord(String word) {
  22.         if (word.length() == 0) {
  23.             System.out.println();
  24.             return;    
  25.         }
  26.         System.out.println(word);
  27.         if ((lastWord.length() != 0 && word.length() != lastWord.length()) || !lexicom.isEnglishWord(word)) {
  28.             System.out.println("Invalid word");
  29.             return;
  30.         }
  31.         int occurrences = 0;
  32.         char[] currWord = word.toCharArray();
  33.         char[] lastWord = this.lastWord.toCharArray();
  34.         for (int i = 0; i < currWord.length; i++) {
  35.             for (int j = 0; j < lastWord.length; j++) {
  36.                 if (currWord[i] == lastWord[i]) {
  37.                     occurrences++;             
  38.                 }          
  39.             }      
  40.         }
  41.         if (occurrences > 1) {
  42.             System.out.println("Invalid word");    
  43.         } else {
  44.             lastWord = word;       
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement