Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-330

Move-to-Front Encoding

Problem

Encode a string over the given alphabet using the move-to-front transform: for each character output its current index in the alphabet and then move it to the front.

Characters not present in the alphabet are undefined behaviour; assume all input characters belong to it. Return the list of indices.

Hint

Frequently used characters drift towards index 0, which is the point of the transform.

Test cases

case 1
in: ["abc","abc"]
expected: [0,1,2]
case 2
in: ["banana","abn"]
expected: [1,1,2,1,1,1]
case 3
in: ["aaa","abc"]
expected: [0,0,0]
case 4
in: ["cab","abc"]
expected: [2,1,2]
case 5
in: ["","abc"]
expected: []
Starter codePython
def move_to_front_encode(s, alphabet):
    # Your code here
    pass

5 test cases · run your code against them in the editor