Sobes.tech
Middle — Senior

Schrijf een functie die het teken met de meeste opeenvolgende herhalingen in een tekenreeks teruggeeft.

sobes.tech AI

Antwoord van AI

def max_consecutive_char(s: str) -> str: if not s: return ''

max_char = s[0]
max_count = 1
current_char = s[0]
current_count = 1

for i in range(1, len(s)):
    if s[i] == current_char:
        current_count += 1
    else:
        if current_count > max_count:
            max_count = current_count
            max_char = current_char
        current_char = s[i]
        current_count = 1

# Controle voor de laatste reeks tekens
if current_count > max_count:
    max_count = current_count
    max_char = current_char

return max_char

Voorbeeld gebruik

print(max_consecutive_char('aabbbccddddde')) # Geeft 'd' terug