Lately, I've been getting used to typing with Swype.
I've noticed certain words can be produced by drawing a straight line from your starting letter to your ending letter, or by skipping letters that repeat.
For example, I can type the word
balloon by Swyping across the following letters:
b > a > l > o > n.
Let us define the Shortest Swype Path, or
SSP, as the minimum number of distinguishable line segments needed to type a string. A line segment is a continuous straight line between any two, or more, letters. Any change in direction starts a new line segment - though some words may be Swyped by drawing only a single straight line.
Use this simple QWERTY keyboard layout:
q w e r t y u i o p a s d f g h j k l z x c v b n m
In the above example, the word
balloon will have an
4 as detailed in the sequence below:
1) Start at `b` (line segments = 0) 2) slide to `a` (line segments = 1) 3) slide to `l` (line segments = 2) 4) slide to `o` (line segments = 3) 5) slide to `n` (line segments = 4)
SSP = 1 since no change of direction is required when Swyping this word.
A single word string containing any
a-z via STDIN, function argument, or command line.
Print via STDOUT, return, or your languages closest alternative, the number
n representing the string's
One trailing newline optional in outut. Standard loopholes disallowed. Shortest submission in bytes wins.
- A change in direction starts a new line segment.
- Letters that repeat are only counted once (e.g.:
bookkeepershould be treated as
- Normally, Swpye corrects missed letters by looking at neighboring letters and filling in its best guess. For this challenge, assume there is no natural language augmentations, predictive text or error correction.
A-Zinputs are treated like their lowercase counterparts.
- Ignore any numbers
0-9in the input.
- Diagonal paths are allowed - that is, a straight line that covers letters
n, for example, count as
1segment. This rule applies to any diagonal slope (e.g.: letters
iare in line).
Input Output --------------------- a 0 aa 0 aaaaaa 0 aaaaaabc 2 in 1 int 2 java 3 qwerty 1 chicago 5 balloon 4 BALLOON 4 typewriter 5 bookkeeper 6 stackexchange 11 2hello7 3 2HELLO7 3