LeetCode 环绕字符串中唯一的子字符串_leetcode


LeetCode 环绕字符串中唯一的子字符串_字符串_02

LeetCode 环绕字符串中唯一的子字符串_动态规划_03


通过双指针的方法,统计每个字符最长的连续字符串(end - strat + 1),start超过end后,本次统计完成。然后将end指向start的位置,开始下一次统计

start会挨个统计每个字符最长的连续字符串,并保存到数组

int findSubstringInWraproundString(string p) {
int start = 0;
int end = 0; // end指向不能连续的第一个字符
vector<int> max_len(26, 0);

while (start < p.size()) {
while (p[end + 1] - p[end] == 1 || p[end + 1] - p[end] == 'a' - 'z') {
// 可以连续
end++;
}
// end指向连续的最后一个
while(start <= end) {
int offset = p[start] - 'a';
max_len[offset] = max(max_len[offset], end - start + 1);
start++;
}

end = start;
}

int num = 0;
for (int len : max_len) {
num += len;
}

return num;
}