alekscooper
alekscooper

Reputation: 831

What is the next string lexicographically?

I'm a bit confused about the lexicographical order of strings.

So if my s = "zzz", what is the next, i.e. "zzz"+1, if I can say so? Is it "zzza" or "azzz"?

Add: my alphabet for this question is [a..z].

Upvotes: 0

Views: 236

Answers (2)

bottaio
bottaio

Reputation: 5093

First of all, when considering lexicographical order you must define the length of a longest word. Unless you do that, getting the next word in order means appending first letter of an alphabet to the previous word. It means that starting from "a" you can't even get to the word "b" in finite number of steps (since words such as "aa", "aaa"... are all before "b" in that ordering).

If you want to generate consecutive words over an alphabet you should use cannonical order in which:

  1. Shorter words are before longer words
  2. If two words have the same length they are sorted alphabetically

In this scenario next word after "zzz" would be "aaaa" which seems a bit counter-intuitive but allows you get the n-th word over the alphabet in a more useful fashion.

Upvotes: 0

Axnyff
Axnyff

Reputation: 9964

It will depend on your character set to know which is the next one but "azzz" is way before "zzz" while "zzza" is after "zzz".

It's the same order as a dictionary: a..z then aa..az then ba..bz etc

Upvotes: 1

Related Questions