力扣题-12.7
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:467. 环绕字符串中唯一的子字符串
解题思想:记录下以字母s[i]结尾的最大的字串个数,然后统计a-z每个字母结尾的最大字串的个数进行i相加
class Solution(object):
def findSubstringInWraproundString(self, s):
"""
:type s: str
:rtype: int
"""
dp = defaultdict(int)
k = 0
for i in range(len(s)):
if i>0 and (ord(s[i])-ord(s[i-1])) %26 == 1:
k += 1
else:
k = 1
dp[s[i]] = max(dp[s[i]],k)
return sum(dp.values())
class Solution {
public:
int findSubstringInWraproundString(string s) {
unordered_map<char, int> dp;
int k=0;
for(int i=0;i<s.size();i++){
if (i > 0 && (s[i] - s[i - 1] + 26) % 26 == 1) {
k++;
}
else{
k = 1;
}
dp[s[i]] = max(dp[s[i]], k);
}
int result = 0;
for (const auto& entry : dp) {
result += entry.second;
}
return result;
}
};