Count Consecutive Characters

Stephen 07/11/2018 at 21:32. 5 answers, 36 views
code-golf string alphabet

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.

5 Answers


ngn 07/11/2018 at 22:40.

APL (Dyalog Classic), 10 9 bytes

-1 byte thanks to H.PWiz

≢⍉↑⊆⍨⎕a∊⍞

Try it online!

inputs a string

⎕a is the uppercase English alphabet

⎕a∊⍞ a boolean length-26 vector - which letters occur in the string?

⊆⍨ form vectors of consecutive 1s

≢⍉↑ mix into a matrix, transpose, and return its height - effectively, find the length of the longest vector of 1s


ngn 07/11/2018 at 21:51.

K (ngn/k), 17 bytes

{#1_{x^x^x+1}\-x}

Try it online!

{ } is a function with argument x

- is there only to convert the chars to numbers (it doesn't matter if they are negative)

{ }\ applies a function until convergence, preserving intermediate results

x+1 add 1 to each number in x

^ "without" - the list on the left without elements that occur in the list on the right

x^x^x+1 means x intersected with x+1

1_ drop the first element

# count


sundar 07/11/2018 at 22:14.

MATL, 12 bytes

Sud1=Y'*X>sQ

Try it online!

Explanation:

     % implicit input (assume 'antidisestablishmentarianism ')
Su   % Sort and unique
     % stack: ['abdehilmnrst']
d    % take the difference between successive characters
     % stack: [1 2 1 3 1 3 1 1 4 1 1]
1=   % check for places where difference was 1 
     % (i.e. alphabetically successive characters were present)
     % stack: [1 0 1 0 1 0 1 1 0 1 1]
Y'   % take the run-length encoding of that
     % stack: [[1 0 1 0 1 0 1 0 1]
               [1 1 1 1 1 1 2 1 2]]
*    % multiply the two elementwise, to zero out
     %  non-successive differences
X>   % take the maximum value from the resulting array
     % stack: [2]
s    % sum that (this is mainly here because 'a' results in
     %  an empty array otherwise - this turns it to 0)
Q    % we have number of differences, increment that to get
     %  number of successive chars
     % stack: [3]
     % implicit output

Sok 07/11/2018 at 22:41.

Pyth, 9 bytes

le}#G.:S{

Input is assumed to be a lower case string. Try it out online here, or verify all the test cases at once here.

le}#G.:S{Q   Q=eval(input()), G=lowercase alphabet. Trailing Q inferred.

        {Q   Deduplicate input string
       S     Sort it
     .:      Take all substrings (these are generated in length order)
  }#G        Filter out those that aren't found in the alphabet
le           Find the length of the last remaining element

Jonathan Allan 07/11/2018 at 22:39.

Jelly,  10  9 bytes

Props to Sok for the method getting -1 byte :)

ṢQẆẇƇØaṪL

Try it online!

Some 10s:

ṢQO_J$ŒgZL
ṢQO_J$Lƙ`Ṁ
ṢQO_J$Lƙ$Ṁ
ṢQOIỊṣ0ZL‘

Related questions

Hot questions

Language

Popular Tags