美文网首页
467. 环绕字符串中唯一的子字符串

467. 环绕字符串中唯一的子字符串

作者: 来到了没有知识的荒原 | 来源:发表于2021-09-24 12:00 被阅读0次

467. 环绕字符串中唯一的子字符串

题解

class Solution {
 public:
  int findSubstringInWraproundString(string p) {
    int n = p.size();
    vector<int> cnt(26, 0);
    int res = 0;
    int len = 1;
    cnt[p[0] - 'a']++;
    for (int i = 1; i < n; i++) {
      if (((p[i - 1] + 1) - 'a') % 26 + 'a' == p[i])
        len++;
      else
        len = 1;
      char c = p[i] - 'a';
      cnt[c] = max(cnt[c], len);
    }
    for (auto i : cnt) res += i;
    return res;
  }
};

相关文章

网友评论

      本文标题:467. 环绕字符串中唯一的子字符串

      本文链接:https://www.haomeiwen.com/subject/etybgltx.html