Advertisement
jbjares

Untitled

Aug 17th, 2015
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package com.jbjares.leetcode;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.LinkedList;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.TreeSet;
  10.  
  11.  
  12. //Given two words (beginWord and endWord),
  13. //and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:
  14.  
  15. //Only one letter can be changed at a time
  16. //Each intermediate word must exist in the dictionary
  17. //For example,
  18. //
  19. //Given:
  20. //start = "hit"
  21. //end = "cog"
  22. //dict = ["hot","dot","dog","lot","log"]
  23. //As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
  24. //return its length 5.
  25. //
  26. //Note:
  27. //Return 0 if there is no such transformation sequence.
  28. //All words have the same length.
  29. //All words contain only lowercase alphabetic characters.
  30.  
  31. public class WordLadder {
  32.  
  33.     public static void main(String[] args) {
  34.        
  35.  
  36.        
  37.         String start = "hit";
  38.         String end = "cog";
  39.        
  40.         LinkedList<String> dictLinkedList = new LinkedList<String>();
  41.         LinkedList<String> resultLinkedList = new LinkedList<String>();
  42.         dictLinkedList.addAll(Arrays.asList(new String[]{"hot","dot","dog","lot","log"}));
  43.        
  44.        
  45.         for(int i=0;i<dictLinkedList.size();i++){
  46.             String actualWord = dictLinkedList.get(i);
  47.            
  48.             for(char c = 'a';c<='z';c++){
  49.                 char[] charArray = actualWord.toCharArray();
  50.                 String testOne = new String(charArray[0]+""+c+""+charArray[2]);
  51.                 String testTwo = new String(c+""+charArray[1]+""+charArray[2]);
  52.                 String testThree = new String(charArray[0]+charArray[1]+c+"");
  53.                                
  54.                 if(dictLinkedList.contains(testOne)){
  55.                     resultLinkedList.add(testOne);                 
  56.                 }
  57.                 if(dictLinkedList.contains(testTwo)){
  58.                     resultLinkedList.add(testTwo);
  59.                 }
  60.                 if(dictLinkedList.contains(testThree)){
  61.                     resultLinkedList.add(testThree);
  62.                 }
  63.                
  64.                 if(start.equals(testOne) || start.equals(testTwo) ||  start.equals(testThree)){
  65.                     resultLinkedList.addFirst(start);
  66.                 }
  67.                 if(end.equals(testOne) || end.equals(testTwo) ||  end.equals(testThree)){
  68.                     resultLinkedList.addLast(end);
  69.                 }
  70.             }
  71.         }
  72.        
  73.         Set<String> resultSet = new HashSet<String>();
  74.         resultSet.addAll(resultLinkedList);
  75.         System.out.println(resultSet);
  76.         System.out.println(resultSet.size());
  77.     }
  78.    
  79.    
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement