409. 最长回文串

image.png

  1. package main
  2. import "fmt"
  3. func longestPalindrome(s string) int {
  4. l := len(s)
  5. m :=make(map[byte]int)
  6. for i:=0;i<l;i++{
  7. m[s[i]]++
  8. }
  9. var res int
  10. for _,v:=range m{
  11. if v/2>0{
  12. res +=(v/2)*2
  13. }
  14. }
  15. if l%2!=0||(l%2==0&&res!=l){
  16. res=res+1
  17. }
  18. return res
  19. }
  20. func main() {
  21. fmt.Println(longestPalindrome("abccccdd"))
  22. fmt.Println(longestPalindrome("ccc"))
  23. }

image.png