23
\$\begingroup\$

Your challenge: Write a function that takes a string s, a character c, and finds the length of the longest run of c in s. The length of the run will be l.

Rules:

  • If s is of length 0 or c is empty, l should be 0.
  • If there are no instances of c in s, l should be 0.
  • Standard loopholes and Standard I/O Rules apply.
  • No matter where in s the run of cs is located, l should be the same.
  • Any printable ASCII characters can appear in s and c.

Test cases:

s,c --> l
"Hello, World!",'l'  -->  2
"Foobar",'o'         -->  2
"abcdef",'e'         -->  1
"three   spaces",' ' -->  3
"xxx xxxx xx",'x'    -->  4
"xxxx xx xxx",'x'    -->  4
"",'a'               -->  0
"anything",''        -->  0

Winner:

As with the shortest answer in each language wins.

\$\endgroup\$
9
  • \$\begingroup\$ Related. Sandbox post. \$\endgroup\$
    – MD XF
    Jun 8, 2017 at 18:01
  • \$\begingroup\$ Could you include the edge cases of empty s and a c that isn't contained in a non-empty s in your test cases? \$\endgroup\$ Jun 8, 2017 at 18:22
  • \$\begingroup\$ What range of characters can appear in s/c? \$\endgroup\$ Jun 8, 2017 at 18:23
  • 11
    \$\begingroup\$ c can be empty? In many languages, a character is just an integer with special semantics, and you can't really have an empty integer either. \$\endgroup\$ Jun 8, 2017 at 18:40
  • 18
    \$\begingroup\$ That doesn't really make sense to me. Your test cases suggest that we have to support it. If we don't have to support it, then specifying its required output doesn't make sense, because I can always just say that it's not supported if my solution would do something else in that case. \$\endgroup\$ Jun 8, 2017 at 18:45

42 Answers 42

1
2
1
\$\begingroup\$

Zsh, 18 bytes

tr -c $1 \\n|wc -L

Try it online!

Explanation:

  • tr: translate characters in STDIN:
    • -c: replace all except
    • $1: the first command-line argument
    • \\n: with newlines
  • |wc -L: then output the length of the longest line
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 11 bytes

Takes arguments in swapped order.

#1_|':\1,^?

Try it online!

^? for each character in s, is it not a member of c?
1, prepend 1.
|':\ until the value stops changing, do pairwise or. This shrinks each run of 0s by 1 per iteration. Collects all intermediate results.
1_ Drop the first value (the initial boolean vector).
# Length. Counts the number of steps it took to fill the vector with 1s.

If we wouldn't have to deal with empty c, this could be 10 bytes instead:

#1_&':\0,=

Try it online!

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

Jelly, 5 bytes

=Œg§Ṁ

Try it online!

-3 bytes thanks to Unrelated String. Port of my Vyxal answer.

This takes a literal character, which can't be input (Jelly treats strings as char arrays), so the footer converts the single-character string into a character. The link can also be called this way.

=     # Is each char of the string equal to the given character?
 Œg   # Group consecutive identical items
   §  # Sum each
    Ṁ # Get the maximum
\$\endgroup\$
1
\$\begingroup\$

Jelly, 5 bytes

ṁ¹ẇƤS

Try it online!

Takes a character on the left and the string on the right. Can also take the character as a length-1 string (and thus be run as a full program), but errors if given a length-0 string, so it's unclear if this is valid given input that way due to one of the most confusing comment clarifications in site history.

ṁ        Mold the character to the length of the string,
 ¹       then
   Ƥ     for each prefix of that result
  ẇ      test if it's a contiguous substring of the string.
    S    Count up how many are.
\$\endgroup\$
0
\$\begingroup\$

Ruby, 40 bytes

->s,c{s.split(%r{[^#{c}]}).max&.size||0}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Clojure, 56 bytes

#(apply max(reductions(fn[r c](if(= c %2)(inc r)0))0 %))
\$\endgroup\$
0
\$\begingroup\$

Arturo, 31 bytes

$[s c]->size max match s~{|c|+}

Try it

\$\endgroup\$
0
\$\begingroup\$

J, 17 bytes

[:>./0(+/;.1)@,e.

Attempt This Online!

[:>./0(+/;.1)@,e.
[:                NB. enforces f(g(y))
     0(+/;.1)@,e. NB. g
               e. NB. check if each char of x is equal to y
     0        ,   NB. prepend a 0
             @    NB. then
      (+/;.1)     NB. cutting and summing
         ;.1      NB. cut up array using first item as delimiter, groups runs of 1's
       +/         NB. sum to get count
  >./             NB. f, get max of result
\$\endgroup\$
0
\$\begingroup\$

Japt, 9 bytes

ôÀV mÊrwT

Try it

\$\endgroup\$
0
\$\begingroup\$

Lua, 89 bytes

s,c=...a=0s:gsub(c:gsub('%W','%%%1')..'+',load'a=math.max(#...,a)')print(#c==0 and 0or a)

Try it online!

Ungolfed version:

s,c=...                           --Take s and c as program arguments
a=0                               --a stores max run of c
s:gsub(c:gsub('%W','%%%1')..'+',  --For every run of c
load'a=math.max(#...,a)')         --If length of c is longer than a, put into a
print(#c==0 and 0or a)            --If c is 0, print 0, otherwise print a
\$\endgroup\$
0
\$\begingroup\$

Scala, 98 bytes

Golfed version. Try it online!

def f(c:Char,s:String)=s.foldLeft((0,0)){case((d,m),x)=>if(x==c)(d+1,Math.max(d+1,m))else(0,m)}._2

Ungolfed version. Try it online!

object Main {
  def f(c: Char, s: String): Int = s.foldLeft((0, 0)) {
    case ((count, maxCount), char) =>
      if (char == c) (count + 1, Math.max(count + 1, maxCount))
      else (0, maxCount)
  }._2

  def main(args: Array[String]): Unit = {
    println(f('c', "ccc cccc cc"))
    println(f('c', ""))
    println(f('c', "abcdef"))
  }
}
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 G, 3 bytes

=ġʂ

Try it online!

Port of Adnan's 05AB1E answer.

Explanation

=ġʂ  # Implicit input
=    # For each character, does it equal the second input?
 ġ   # Group consecutive equal items together
  ʂ  # Sum each inner list
     # Implicit output of maximum
\$\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.