😕题目描述

😔分析

zabxyz,可以分为z、za、zab、x、xy、xyz。

  • 以z结尾有一个;
  • 以a结尾有两个;
  • 以b结尾有三个;
  • 以x结尾有一个;
  • 以y结尾有两个;
  • 以z结尾有三个;

此时需要去重z,所以对以z结尾的取最大值。所以就只有2+3+1+2+3。对26个字母结尾的个数都要取最大值。

😏答案

  1. func findSubstringInWraproundString(p string) int {
  2. maxLen := make([]int, 26)
  3. res := 0
  4. count := 0
  5. for i, _ := range p {
  6. if i == 0 {
  7. count++
  8. maxLen[p[i]-'a'] = max467(maxLen[p[i]-'a'], count)
  9. continue
  10. }
  11. if p[i] == p[i-1]+1 || (p[i] == 'a' && p[i-1] == 'z') {
  12. count++
  13. } else {
  14. count = 1
  15. }
  16. maxLen[p[i]-'a'] = max467(maxLen[p[i]-'a'], count)
  17. }
  18. for i := 0; i < len(maxLen); i++ {
  19. fmt.Print(maxLen[i], " ")
  20. res += maxLen[i]
  21. }
  22. return res
  23. }
  24. func max467(ls, rs int) int {
  25. if ls > rs {
  26. return ls
  27. }
  28. return rs
  29. }