-3

I'm trying to figure out how to check if certain characters are repeated after each other in a single string, and if so, how often are they repeated.

Example:

str.x = 'abbbjjaaaal'

As the return I need the integer 4, as in this case the longest consecutive repetition of a single character in the string x is a, and it is repeated 4 times.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • There are many approaches to this. Grouping, regular expressions or just plain old `for` loops with some counting. Which of these have you tried already, and where is your code? – timgeb Sep 21 '18 at 14:17

1 Answers1

0
some_str = 'abbbjjaaaal'
groups = [(k , len(list(g))) for k, g in groupby(a, str)]
groups.sort(key=lambda k:k[1], reverse=True)
print(grups[0][0])
shahaf
  • 4,750
  • 2
  • 29
  • 32