467. 环绕字符串中唯一的子字符串
https://leetcode.cn/problems/unique-substrings-in-wraparound-string/
难度中等348
把字符串 s 看作 "abcdefghijklmnopqrstuvwxyz" 的无限环绕字符串,所以 s 看起来是这样的:
"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...."。
现在给定另一个字符串 p 。返回 s 中 不同 的 p 的 非空子串 的数量 。
示例 1:
输入:p = "a" 输出:1 解释:字符串 s 中只有 p 的一个 "a" 子字符。
示例 2:
输入:p = "cac"
输出:2
解释:字符串 s 中只有 p 的两个子串 ("a", "c") 。
示例 3:
输入:p = "zab"
输出:6
解释:在字符串 s 中有 p 的六个子串 ("z", "a", "b", "za", "ab", "zab") 。
提示:
1 <= p.length <= 105p由小写英文字母组成
通过次数33,517提交次数64,765
class Solution {
public int findSubstringInWraproundString(String p) {
int [] dp = new int[26];
int n =0;
for(int i=0;i<p.length();i++)
{
if(i>0 && (p.charAt(i-1)+1==p.charAt(i)||p.charAt(i-1)=='z'&&p.charAt(i)=='a')||p.charAt(p.length()-1)=='z'&&p.charAt(0)=='a')
{
++n;
}
else n=1;
dp[p.charAt(i)-'a'] = Math.max(dp[p.charAt(i)-'a'],n);
}
int ans = 0;
for(int i=0;i<26;i++) ans+=dp[i];
return ans;
}
}

© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持一下吧
相关推荐








暂无评论内容