Sobes.tech
Middle — Senior

Írjon egy függvényt, amely visszaadja a karaktert a legtöbb egymást követő ismétléssel egy sztringben.

sobes.tech MI

Válasz az MI-től

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

# Utolsó karakterlánc ellenőrzése
if current_count > max_count:
    max_count = current_count
    max_char = current_char

return max_char

Használati példa

print(max_consecutive_char('aabbbccddddde')) # 'd' kiírása