Single letter swaps
Write a function that takes a sequence of strings and a target string. For each string in the sequence, determine if it is equal to the target string after exactly one letter swap. Return the sequence of letter pairs that are swapped, or nil if it doesn't exist.
Example
(letter-swaps ["bacd" "abdc" "abcde" "abcc" "abcd"] "abcd")
;=> [#{\a \b} #{\c \d} nil nil nil]
Swapping a and b in "bacd" gives you the target string. Swapping c and d in "abdc" gives you the target string. But there is no way to swap to get an extra e. And trading a d for a c is not possible. Finally, the last string has no swaps, and exactly one is required.
Thanks to this site for the problem idea, where it is rated Very Hard in PHP. The problem has been modified.
Please submit your solutions as comments on this gist.
To subscribe: https://purelyfunctional.tv/newsletter/
2nd solution (the first failed on the "bac" case).
I tried rewriting it to match how I'd solve the problem as a human, then flattened the nested nightmare of ifs and merged two variables into one by abusing dynamic types.
I like that this solution doesn't look at the whole string if it doesn't need to.