Sobes.tech
Principal

/** * Caractères répétés * * Il faut écrire une fonction qui prend une chaîne en entrée et retourne * pour chaque caractère unique, le nombre maximal de répétitions consécutives. * * Entrée : qqffbbaaaffc * Sortie : a:4 b:1 f:2 c:1 * * Entrée : bbbbbb * Sortie : b:6 * * Entrée : abc * Sortie : a:1 b:1 c:1 * * Entrée : aabbcc * Sortie : a:2 b:2 c:2 * * Entrée : aaabbbccc * Sortie : a:3 b:3 c:3 * * Entrée : a * Sortie : a:1 * * Entrée : "" * Sortie: */

sobes.tech IA

Réponse de l'IA

go package main

import ( "fmt" )

func maxRepeats(s string) map[rune]int { result := make(map[rune]int) if len(s) == 0 { return result }

var prev rune
count := 0

for i, ch := range s {
	if i == 0 {
		prev = ch
		count = 1
		continue
	}

	if ch == prev {
		count++
	} else {
		if count > result[prev] {
			result[prev] = count
		}
		prev = ch
		count = 1
	}
}

// Проверяем последний символ
if count > result[prev] {
	result[prev] = count
}

return result

}

func main() { tests := []string{ "qqffbbaaaffc", "bbbbbb", "abc", "aabbcc", "aaabbbccc", "a", "", }

for _, test := range tests {
	res := maxRepeats(test)
	fmt.Printf("Input: %s\nOutput: ", test)
	for ch, cnt := range res {
		fmt.Printf("%c:%d ", ch, cnt)
	}
	fmt.Println("\n")
}

}