cswork / libraryOpen practice workspace

Coding questions · Snapchat

Determine Whether a Word Transformation Exists

This question evaluates a candidate's ability to model single-character word transformations as a graph/search problem, testing competencies in graph traversal, string manipulation, and state-space connectivity within the Coding & Algorithms domain.

Given a beginWord, an endWord, and a list of valid words wordList, determine whether it is possible to transform beginWord into endWord.

Rules:

  • Each transformation changes exactly one character.
  • Every transformed word must exist in wordList.
  • All words contain only lowercase English letters and have the same length.
  • Return true if at least one valid transformation sequence exists; otherwise return false.

Example:

  • Input: beginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log", "cog"]
  • Output: true

Because one valid sequence is: hit -> hot -> dot -> dog -> cog

You only need to return a boolean value, not the length of the shortest transformation.

Word Transformation Exists

Return whether beginWord can reach endWord by changing one character at a time through wordList words.

Hints

  1. Bucket words by wildcard patterns.
  2. Run BFS from beginWord until endWord is found.