TLDR: Here's a constant-time solution: http://codepad.org/Id9eu0Ax
Matching the the sequence of fizz/buzz/fizbuzz disregarding numbers is a repetitive case.
Start with
3 Fizz
5 Buzz
6 Fizz
9 Fizz
10 Buzz
12 Fizz
15 FizzBuzz
18 Fizz
20 Buzz
21 Fizz
24 Fizz
25 Buzz
27 Fizz
30 FizzBuzz
[repeats]
Just split the input list as a prefix of a few, and then a repeat. The goal of the shortest total distance between them is constant. You just need to measure the number of ints between each of the 8 repeating distances.For example:
3 -> Fizz
+2 -> Buzz
+1 -> Fizz
+3 -> Fizz
+1 -> Buzz
+2 -> Fizz
+3 -> FizzBuzz
+3 -> Fizz
+2 -> Buzz
+1 -> Fizz
+3 -> Fizz
+1 -> Buzz
+2 -> Fizz
+3 -> FizzBuzz
So you would match against the list [Fizz, Buzz, Fizz, Fizz, Buzz, Fizz, FizzBuzz].