class Pair { String word; int step; Pair(String word, int step) { this.word = word; this.step = step; } } class Solution { public int ladderLength(String beginWord, String endWord, List wordList) { // Step-1 Storing unique words from word list into set data structure HashSet set = new HashSet<>(); for(String word: wordList) set.add(word); // Step-2 suppose endword itself is not present in wordlist then return NO PATH FOUND if(!set.contains(endWord)) return 0; // Step-2 Define a queue and store the , we will start with beginWord with step taken as 1 Queue q = new LinkedList<>(); q.add(new Pair(beginWord, 1)); // Step-4 Process the words stored in Queue while(!q.isEmpty()) { Pair pair = q.poll(); String word = pair.word; int step = pair.step; // if we have reached the end word then return the steps taken to reach end word if(word.equals(endWord)) return step; // suppose word is "hit" // traversing through each character // and see if new word exists in set or not for(int i=0; i a,a,t -> b,a,t -> c,b,t... -> ca,a.... // q: { (bat, 2) } // b,a,t -> a,a,t..-> b,b,t...-> ba,g // q: { (bag, 3) } // b,a,g -> a,a,g...s,a,g // q: { (sag, 4) } // s,a,g --> endWord return steps