# Is this word in standard order?

Let $$\ A \$$ represent the alphabet, such that $$\ A_1 = \$$ a and $$\ A_{26} = \$$ z.

Let's define that a word $$\ W = w_1 w_2 ... w_n \$$ (where $$\ w_c \in A\$$) is in standard order if and only if:

• $$\ w_1 = A_1 \$$, and
• for $$\ 2 \le i \le n \$$, if $$\ w_i = A_x \$$ then $$\ w_j = A_{x-1} \$$ for some $$\ j < i \$$ and some $$\x\$$.

In other words, the word must start with a and each other letter can only appear in a word if the preceding letter in the alphabet has already appeared. Equivalently, if we take only the first appearance of each unique letter in the word, the resulting word is a prefix of the alphabet.

For example, ac is not in standard order, because there is no b before the c.

The following relationships exist between the property of standard order and some others (this list is mainly here for searchability):

• A word is a valid rhyme scheme if and only if it is in standard order (related challenge)
• A word in standard order is the lexicographically earliest among all its isomorphs
• The number of words of length $$\ n \$$ which are in standard order is the $$\ n \$$th Bell number (related challenge)

Given a string of letters, determine if it is in standard order according to the Latin alphabet.

## Test cases

Truthy:

a
aaa
abab
aabcc
abcdefghijklmnopqrstuvwxyzh


Falsey:

b
ac
bac
abbdc
bcdefghijklmnopqrstuvwxyza


## Rules

• You should represent true and false outputs using any two distinct values of your choice
• You may assume the input is non-empty and only contains lowercase ASCII letters
• Alternatively, you may accept input as a list of integers representing alphabet indices (in either $$\ [0, 25] \$$ or $$\ [1, 26] \$$, at your option)
• You may use any standard I/O method
• Standard loopholes are forbidden
• This is , so the shortest code in bytes wins
• Sandbox Jan 20, 2022 at 18:21

# JavaScript (Node.js), 27 bytes

a=>a.every(n=>a[-n]=a[1-n])


Try it online!

Input as an array of 1~26 integers.

• How on earth does this work‽ Jan 21, 2022 at 6:48
• @pxeger I think it's something to do with JS letting you actually set negative indices (rather than having wrap-around indexing like Python), so as it encounters letters it sets the corresponding negative index to undefined if the previous one hasn't been seen and... some truthy thing? if it has. Feel like there might be a counterexample bvut it's still clever as fuck Jan 21, 2022 at 7:04
• @UnrelatedString The truthy thing is a, the first letter of input.
– tsh
Jan 21, 2022 at 10:24

# x86 32-bit machine code, 13 bytes

b0 61 38 02 7f 05 1c ff 42 e2 f7 d6 c3


Try it online!

Following the fastcall calling convention, this takes the length of the string in ECX and the address of the string in EDX, and returns in AL, -1 for standard order and 0 for not standard order.

Assembly:

f:  mov al, 0x61    # 'a' -- AL will hold the highest acceptable letter.
r:  cmp [edx], al   # Compare -- Letting c be the current letter, calculate c - AL.
jg b            # If c > AL, the word is not in standard order; jump out.
sbb al, 0xFF    # Here, the carry flag CF is 1 if c < AL and 0 if c = AL.
# Subtract 255+CF from AL. This leaves AL unchanged if c < AL
#  while increasing it by 1 if c = AL.
inc edx         # Advance the pointer.
loop r          # Loop, counting down from the length in ECX.
b:  .byte 0xD6      # Undocumented SALC instruction -- sets AL to -CF.
#  If the word is not in standard order, CF = 0 from the CMP.
#  If it is in standard order, CF = 1 from the SBB,
#   as INC and LOOP leave CF unchanged.
ret             # Return.


# Jelly, 3 bytes

QJƑ


Try It Online!

Takes input as one-indexed alphabet indices.

QJƑ    Main Link
Q      Uniquify
JƑ    Is it equal to [1, 2, 3, ..., len]?

• Ha, even our explanations are almost exactly the same. Jan 20, 2022 at 18:48

# Vyxal, 4 bytes

U:ż⁼


Try it Online!

Takes a list of nonnegative integers.

U    # Uniquify
⁼ # Is equal to
:ż  # 1...length?


# Python 3, 39 32 bytes

Thanks @xnor for saving 2 bytes by proposing this approach (going from right to lefft)
Thanks @loopy walt for golfing a further 3 bytes

def f(L):len({*L})>L.pop()!=f(L)


Try it online!

Takes in a list of integers from 0 to 25. Throws exception if the numbers are in standard order, otherwise terminates without exception (aka returns through exit code).

Check if the last element is less than the number of unique elements in the list:

• If true, recurs on the remaining list (after the last element has been removed). If the list is in order, then we'll eventually run out of elements after some recursions, throwing an exception on L.pop().
• If false, then that last element must not be in order, in which case the comparison short circuited and the function terminates.
• I don't think terminating by exit code per se is allowed for functions because they must be reusable, but anyway you're covered by the more direct rule allowing exceptions for boolean outputs. Jan 20, 2022 at 20:40
• 37 bytes by going from right to left
– xnor
Jan 21, 2022 at 2:20
• 34 Jan 21, 2022 at 3:21
• best one yet surculose! Jan 21, 2022 at 13:10

# JavaScript (V8), 35 bytes

a=>[...new Set(a)].some((x,i)=>x^i)


Try it online!

takes input as an array with integers representing 0-indexed alphabet indices. returns false for valid words, and true for invalid words

# JavaScript (ES6), 30 bytes

Expects a list of 0-based indices. Returns false for valid or true for invalid.

a=>a.some(c=>c*!a[a[-c-2]=~c])


Try it online!

### Commented

a =>            // a[] = input array, re-used as an object to keep track of
//       characters that have already appeared in the list
a.some(c =>     // for each code c in a[]:
c *           //   return true if c is not equal to 0
!a[           //   and the following entry is defined:
a[-c - 2] = //     set a[-c - 2] to a non-zero value
~c          //     use -c - 1 as the lookup value
]             //   end of test
)               // end of some()


# Wolfram Language (Mathematica), 26 bytes

(l=#)&@@@Tally@#==Range@l&


Try it online!

Input a list of alphabet indices, 1-indexed.

   # &@@@Tally@#            deduplicate
(l= )           ==Range@l   equal to (1...(last unique value))?


# 05AB1E, 3 bytes

ÙāQ


Input as a 1-based integer-list.

If we take the actual lowercase letters as input, it would have been 5 bytes instead:

AIÙÅ?


Explanation:

Ù     # Uniquify the (implicit) input-list
ā    # Push a list in the range [1,length] (without popping)
Q   # Check if both lists are equal
# (after which the result is output implicitly)

A     # Push the lowercase alphabet
I    # Push the input-string
Ù   # Uniquify this input-string
Å? # Check if the lowercase alphabet starts with this uniquified input
# (after which the result is output implicitly)


# Haskell + hgl, 9 bytes

fsw β<nb


## Explanation

nb takes the string and removes all but the first occurrence of every character. fsw takes two lists and determines if the first one starts with the second. β is the lowercase alphabet. So all in all this asks if the first occurrence of every character forms a prefix of the alphabet.

You can replace β with nn for numbers 0 through 25 and nN for numbers 1 through 26.

## Reflections

This does well. For once there's not too much to say here.

• An "is this sorted" builtin might be nice but because the string must start with a it would require that as a second check and it wouldn't save anything on this challenge.
• I also noticed there is no upper case alphabet builtin, which there should probably be. It doesn't make a difference to this answer though.

# Factor, 31 30 bytes

[ members 26 iota swap head? ]


Try it online!

## Explanation

Get the unique elements of the input, preserving order, and check whether that is a prefix of the alphabet(ic indices).

          ! { 0 1 0 1 }
members   ! { 0 1 }
26 iota   ! { 0 1 } { 0 1 2 ... 25 }
swap      ! { 0 1 2 ... 25 } { 0 1 }


# Nibbles, 5 bytes (9 nibbles)

==;$_,,@  ## Verbose == # Equals? ; $ _   #   Uniq of input, save it
, , @   # Range from 1 to length of saved value


## Variant with alphabet (8 bytes / 15 nibbles)

==.;$@-$'',,@


### Verbose

==   # Equals?
.   # Map
; $@ # Uniq of input, save it -$ ''   #   Subtract ''
, , @   # Range from 1 to length of saved value


# R, 35 bytes

function(W)any(seq(u<-unique(W))-u)


Try it online!

Takes input as a vector of integers from 1 to 26. Outputs FALSE for truthy and TRUE for falsey.

• Can't R compare vectors for equality directly, rather than using any + not-equal? Jan 20, 2022 at 21:09
• @pgexer, nope, it's elementwise with recycling if necessary (unfortunately in this problem). Jan 20, 2022 at 21:10

# Brachylog, 3 bytes

d~⟦


Try it online!

Takes 0-indices into the alphabet.

d      The nub of the input
~⟦    is the range from 0 to something.


# Brachylog, 5 bytes

d~a₀Ạ


Try it online!

Takes a string of lowercase letters.

d        The nub of the input
~a₀     is a prefix of
Ạ    the lowercase alphabet.


# Shue, 67 65 bytes

Shue confirmed less verbose than Java ????

Takes input as a list of unary numbers (1-26) separated by ",", with a trailing ",". Returns "LR" for yes, and "FR" for no.

=L
=R
L1,=L>,
L,=L
>1=1>
>,1=,>
,,=,
>,R=,R
LR
L11=F
F1=F
F,=F
FR


Try it online!

# Explanation

=L      - Left edge
=R      - Right edge
L1,=L>, - Decrement the first element, which is 1, create triangle
L,=L    - Remove a 0 without creating a triangle
>1=1>   - Triangle passes trough ones
>,1=,>  - Triangle decrements the next element
,,=,    - Remove zeros since they don't affect the result
>,R=,R  - Delete triangle when it reaches the end
LR      - Success
L11=F   - Failure, the first element is 2 or greater
F1=F    - Propagate failure
F,=F    - Propagate failure
FR      - Failure


# Rough pseudocode

def is_valid(array):
if array == []:
return True
if array == 0:
return is_valid(array[1:])
elif array == 1:
return is_valid(array[1:]-1)
else:
return False


# BQN, 3 bytesSBCS

Takes input as a list in $$\[0, 25]\$$.

≡⟜⊐


Run online!

BQN's primitive Classify ⊐ basically converts a sequence to "standard order" in non-negative integers. As the input uses the same domain we can simply check if the result of that matches the input.

# J, 9 bytes

(-:#\)@~.


Try it online!

Input is 1-based indices of the alphabet letters.

• @~. Take the unique (preserves order) and...
• (-:#\) Check if it equals 1 2 3 ... <length of uniq>.

# SNOBOL4 (CSNOBOL4), 90 bytes

	I =INPUT
N	I LEN(1) . X	:F(O)
O =O X
&LCASE O	:F(END)
R	I X =	:S(R)F(N)
O	OUTPUT =1
END


Try it online!

Same "check if unique characters are a prefix of the alphabet" algorithm others have posted.

# Retina 0.8.2, 30 bytes

D.
^
$'¶ Tl@l^.+ ^@(.*)¶\1  Try it online! Link includes test cases. Explanation: D.  Remove duplicate letters. ^$'¶


Duplicate the remaining letters.

Tl@l^.+


Shift the letters in the first copy back by 1, e.g. abcd becomes @abc.

^@(.*)¶\1


Check that the second copy starts with the tail of the first copy, which is only possible if this is a prefix of the alphabet.

# Charcoal, 10 bytes

¬⌕βΦθ⁼κ⌕θι


Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for standard order, nothing if not. Explanation:

 ⌕          Index of
θ       Input string
Φ        Filtered where
κ     Current index
⁼      Equals
⌕    First index of
ι  Current letter
θ   In input string
β         In predefined variable lowercase alphabet
¬           Equals zero
Implicitly print


# C (gcc), 66 63 62 bytes

r;m;f(char*s){for(r=0,m=96;*s;++s)r|=*s-m==1?m=*s,0:*s>m;m=r;}


Try it online!

Inputs a word as a string.
Returns $$\0\$$ if the word is in standard order or $$\1\$$ otherwise.

# Java, 68 bytes

l->{int[]x={0};return l.stream().distinct().allMatch(i->i==x++);}


Takes input as a List of alphabet indices.

Try it online!

# Python 3 - 47 bytes

Thanks to @pxeger and @m90

lambda a:[*range(len(u:={}.fromkeys(a)))]==[*u]


Input is a list of integers from [0,25]. All it does is remove duplicates and makes sure that each integer is equal to its position. Output is inverted, which is allowed.

Attempt it online!

## Python 3.8 - 50 bytes

Thanks to @pxeger for -12 bytes here.

lambda a,i=0:any(k^(i:=i+1)for k in{}.fromkeys(a))


Input is a list of integers from [1,26]. All it does is remove duplicates and makes sure that each integer is equal to its position. Output is inverted, which is allowed.

Try it online! (Function output is inverted, but I inverted it back in the footer of the interpreter)

## Python 3 - 58 bytes

Thanks to @m90.

lambda a:0in map(int.__eq__,{}.fromkeys(a),range(len(a)))


Input is a list of integers from [0,25]. All it does is remove duplicates and makes sure that each integer is equal to its position. Outputs are inverted, which is allowed.

Try it online!

• dict.fromkeys can be {}.fromkeys: see this tip of mine Jan 20, 2022 at 19:45
• If you change i==j to i^j and change all to any, you can save a byte (output becomes inverted, but that's allowed) Jan 20, 2022 at 19:50
• Improvement to 58: lambda a:all(map(int.__eq__,{}.fromkeys(a),range(len(a))))
– m90
Jan 20, 2022 at 19:54
• I just realised you can remove both sets of [] from your original answer, which makes it shorter overall: lambda a:any(i^j for i,j in enumerate({}.fromkeys(a))) Jan 20, 2022 at 20:07
• down to 47 bytes by combining both answers Jan 20, 2022 at 20:49

# Dyalog APL, 7 bytes

⍳∘≢∘∪≡∪
`

Takes input as integers from 1 to 26.

Try it online!