Problem · String
HardGoldman SachsPHONE SCREEN

Problem statement

Given beginWord, endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord.

Every transformation changes exactly one letter, and every transformed word, including endWord, must be in the dictionary. Return 0 when no sequence exists.

Function

ladderLength(beginWord: String, endWord: String, wordList: String[]) → int

Examples

Example 1

beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log","cog"]return = 5

A shortest sequence is hit -> hot -> dot -> dog -> cog.

Example 2

beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log"]return = 0

More Goldman Sachs problems

drafts saved locally
public int ladderLength(String beginWord, String endWord, String[] wordList) {
  // write your code here
}
beginWord"hit"
endWord"cog"
wordList["hot","dot","dog","lot","log","cog"]
expected5
checking account