Advertisement
Guest User

Untitled

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