Sobes.tech
Middle — Senior

How to choose a symbol when there are multiple consecutive maximum symbols?

sobes.tech AI

Answer from AI

If a string contains multiple consecutive maximum-length sequences of identical characters, and you need to choose one character, the following approaches are usually applied:

  • Choose the character from the first maximum sequence (left priority).
  • Choose the character from the last maximum sequence (right priority).
  • Choose the character based on an additional criterion, such as lexicographically minimal or maximal.

An example in Python, where the character from the first maximum sequence is selected:

s = "aaabbccccddeeeeee"
max_len = 0
max_char = ''
current_char = ''
current_len = 0

for c in s:
    if c == current_char:
        current_len += 1
    else:
        if current_len > max_len:
            max_len = current_len
            max_char = current_char
        current_char = c
        current_len = 1

# Check for the last sequence
if current_len > max_len:
    max_len = current_len
    max_char = current_char

print(f"Character with the maximum consecutive sequence: {max_char}, length: {max_len}")

If you need to select among multiple maximum sequences, you can store all such characters and apply additional selection logic.