28
\$\begingroup\$

Given a string containing only letters, output the length of the longest run of consecutive alphabetical letters the word contains, where order does not matter. An example algorithm may sort the word, remove duplicates, and then output the length of the longest run.

Test Cases

watch -> 1
stars -> 3
antidisestablishmentarianism -> 3
backdoor -> 4
a -> 1
tutorials -> 4

For example, antidisestablishmentarianism contains the letters abdehilmnstr. The longest runs are lmn and rst, both of length 3.

Notes

You may take all lowercase, all uppercase, or mixed-case letters as input, but the case cannot encode information about the word (i.e. you cannot make the first n characters capitalized where n is the length of the longest run).

This is , so shortest answer in bytes wins.

\$\endgroup\$
7
  • \$\begingroup\$ @H.PWiz, I'm guessing that's a typo and it should be rst - uniquify, sort and get longest consecutive run. Can we take input as an array of characters? \$\endgroup\$
    – Shaggy
    Jul 11, 2018 at 21:46
  • \$\begingroup\$ @Shaggy yes, definitely, I didn't include it because I thought it was a default \$\endgroup\$
    – Stephen
    Jul 11, 2018 at 21:51
  • \$\begingroup\$ Is 'a' adjacent to 'z' -- should 'zebra' score 2 or 3? \$\endgroup\$ Jul 11, 2018 at 21:54
  • \$\begingroup\$ (...judging by your example algorithm I guess "no" and "2") \$\endgroup\$ Jul 11, 2018 at 22:00
  • \$\begingroup\$ @JonathanAllan you are correct \$\endgroup\$
    – Stephen
    Jul 11, 2018 at 22:05

36 Answers 36

1
2
1
\$\begingroup\$

Kotlin, 97 bytes

{w:String->{var s=0
var m=0
for(c in 'a'..'z')if(c in w)s++
else{if(s>m)m=s
s=0}
if(s>m)m=s
m}()}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java 8, 77 bytes

int i,j,m;
c->{for(i=j=0;(m=j<c.length?m|1<<c[j++]:m&m*2+0*++i)>0;);return i;}

Port of Arnauld's C answer. Try it online here.

Ungolfed:

int i, j, m; // instance variables of the surrounding class - initialised to 0
c -> { // lambda - c is of type char[]; return type is int
    for(i = j = 0; // i is the length of the longest run, j is used to step through c - both start at 0
        (m = j < c.length // work with the bitmask of all the letters present in c: if we have not reached the end of c ...
             ? m | 1 << c[j++] // ... set the bit corresponding to the current character, advance one character ...
             : m & m * 2 + 0 * ++i) > 0 ;) ; // ... else reduce runs of consecutively set bits in m by AND-combining it with a left-shifted copy of itself until m hits 0
    return i; // return the result - by now m is back to 0
}
\$\endgroup\$
1
\$\begingroup\$

><>, 63 bytes

Reads lowercase characters from stdin, outputs a number to stdout.

0l55*)?\
8/?(0:i<]r1~r[-*c
~/00
}</?)@:{:*+1/?(3l
  \~:03. ;n~/

Try it online!

0l55*)?\             Push 26 zeroes onto the stack

Record which characters are used
      i              Read a character from the input
 /?(0:               Check if it is -1, marking the end of the input
8             -*c    Subtract 96 from the character code, 
                         giving 1 for 'a', 2 for 'b' etc.
            r[       Pop that many values on to a new stack and reverse 
                         it, putting that character's value at the top of 
                         the stack
          1~         Write 1 to that value
        ]r           Return the stack back to it's normal state

Count the longest run of ones in the stack
  00                 Push values for currentRun = 0, and bestRun = 0
}                    Move bestRun to the bottom of the stack
            /?(3l    Check if there are only 2 values left on the stack
          +1         Increment currentRun
         *           Multiply currentRun by the next value in the stack, 
                         resetting it to 0 if the run is broken
  /?)@:{:            Check if currentRun > bestRun
  \~:                Overwrite bestRun if so
     03.             Jump back to the start of loop
         ;n~/        Once all values have been consumed, 
                         print bestRun and exit
\$\endgroup\$
1
\$\begingroup\$

Husk, 7 bytes

L►Lġ≈Ou

Try it online!

First time I've used simil(≈), and it works really well!

Explanation

L►Lġ≈Ou 
     Ou order and deduplicate
   ġ≈   group by absolute difference ≤ 1
 ►L     max by length
L       take the length of that
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 63 bytes

Length[Alphabet[]~LongestCommonSubsequence~Union@Characters@#]&

View it on Wolfram Cloud!

Mathematica's verbosity is a blessing and a curse, as usual. LongestCommonSubsequence is exactly the right tool for the job, but its an astounding 24 bytes! (Also, as usual, strings being atoms is frustrating.)

\$\endgroup\$
1
\$\begingroup\$

Thunno 2 G, 5 bytes

Ạ€Ƈġʂ

Try it online!

Port of Adnan's 05AB1E answer.

Explanation

Ạ€Ƈġʂ  # Implicit input
Ạ      # Push the lowercase alphabet
 €     # For each letter in this string:
  Ƈ    #  Is it in the input string?
   ġ   # Group consecutive items together
    ʂ  # Sum each inner group
       # Implicit output of maximum sum
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.