Alex Beals
Alex Beals

Reputation: 2135

Algorithm to convert string to int while mainting lexicographical order

I need an algorithm that will map a string of length four to a four digit number. The problem is it has to maintain lexicographical order. It doesn't need to not have collisions, it's just that if S <= T, F(S) <= F(T). The only additional constraint is that it should be surjective: all numbers must be mapped to by some string.

Upvotes: 1

Views: 1136

Answers (1)

MattPutnam
MattPutnam

Reputation: 3017

Can't you just map letters to digits directly, grouping a few at a time to handle the fact that there are more letters than digits? {a, b, c} => 1, {d, e, f} => 2 etc.

Upvotes: 1

Related Questions