2 of 6
added 69 characters in body
Martin Ender
  • 196.8k
  • 65
  • 444
  • 970

Smallest distance to a palindrome containing a substring

This was inspired by a now removed CS.SE question.

Task

Given two non-empty input strings A and B, output the smallest distance from A to a palindrome that contains B as a substring. Distance is defined by the number of character replacements (Hamming distance).

Restrictions

  • Sensible input: a palindrome exists. This means |A| ≥ |B|.
  • A and B contain only lower ASCII characters, lowercase and uppercase are distinct (as are all other characters).
  • You may take input from stdin, function arguments, command line arguments, etc.
  • You may give the result on stdout, return value, etc.
  • You do not need to give a working palindrome, the smallest distance to one is sufficient.

Examples

A                   B            Output
thilloaoyreot       hello        4 (thelloaolleht)
benjonson           stack        9 (stackcats)
neversaynever!      odd          9 (neveroddoreven)

Scoring

This is code golf, shortest code in bytes wins.

user42682