WordPuzzle
TCCC '04 Wildcard · 2004-02-23 · by schveiguy
Problem Statement
In some popular word puzzles from newspapers, you are to convert a word into another word by either swapping letters, or changing one letter to another. However, one constraint is that the characters formed by each move MUST be a real word.
Your method will be given a
Notes
- The words in the input might or might not be actual English words or words from any other language. See example 3.
Constraints
- dictionary has between 1 and 50 elements, inclusive.
- source has between 4 and 50 characters, inclusive.
- target has the same number of characters as source.
- All elements in dictionary have the same number of characters as source.
- source, target, and all elements of dictionary will consist only of lowercase letters ('a'-'z').
- source will not be the same as target.
- source and target will not appear in dictionary.
- There will be no repeated elements in dictionary.
{"fowl","foal","loaf","load","loan","loon","toon"}
"foul"
"tool"
Returns: { "foul", "foal", "loaf", "loan", "loon", "toon", "tool" }
Note that although in the given list of words, each element can achieve the next, you can skip over "fowl" and "load" since you can go directly from "foul" to "foal", and directly from "loaf" to "loan".
{"stew","wets"}
"stow"
"west"
Returns: { }
Only two letters can be swapped at once.
{"flew", "slew", "stew", "stow"}
"flow"
"slow"
Returns: { "flow", "slow" }
In this conversion, no words from the dictionary were necessary.
{"tsray","trsay","trasy"}
"stray"
"trays"
Returns: { "stray", "tsray", "trsay", "trasy", "trays" }
Note that you cannot simply remove the 's' and insert it at the end.
{"toon","loot"}
"loon"
"tool"
Returns: { "loon", "loot", "tool" }
The following is also a conversion with three words, but is lexicographically larger than the answer: loon -> toon -> tool
Submissions are judged against all 74 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class WordPuzzle with a public method vector<string> getConversions(vector<string> dictionary, string source, string target) · 74 test cases · 2 s / 256 MB per case