Tr: 17 13 characters
(Or 14 10 if you count only the arguments…)
tr -s ta-s -1
Sample run:
bash-4.3$ tr -s ta-s -1 <<< heads
1
bash-4.3$ tr -s ta-s -1 <<< tails
-1
Brief explanation:
tr
stands for transliterate, that means, replaces each character of the input found in the first argument with character at the same position in the second argument:
tr ta -1 <<< tails # replaces t ⇢ -, a → 1
⇒ -1ils
If the first argument is longer, the characters without positional match in the second argument are replaced with second argument's last character:
tr tals -1 <<< tails # replaces t ⇢ -, a → 1, l → 1, s → 1
⇒ -1i11
When -s
(--squeeze-repeats
) option is used, successive characters which would be replaced with the same character are replaced at once:
tr -s tals -1 <<< tails # replaces t ⇢ -, a → 1, l+s → 1
⇒ -1i1
So if we enumerate all characters in “tails”, we get what we need:
tr -s tails -1 <<< tails # replaces t ⇢ -, a+i+l+s → 1
⇒ -1
Same for “heads”, but wee need to keep the “t” in front to consume the minus (characters sorted alphabetically for creepiness):
tr -s taedhs -1 <<< heads # replaces h+e+a+d+s → 1
⇒ 1
Merging all uniques characters of “tails” and “heads” in a single first argument, keeping “t” in front leads to final solution:
tr -s tadehils -1 <<< tails # replaces t → -, a+i+l+s → 1
⇒ -1
tr -s tadehils -1 <<< heads # replaces h+e+a+d+s → 1
⇒ 1
To avoid enumerating the characters, an interval in from-to format can be used instead.