//
把字符串 s
看作是 “abcdefghijklmnopqrstuvwxyz”
的无限环绕字符串,所以 s
看起来是这样的:
//
//
// "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...."
.
//
//
//现在给定另一个字符串 p
。返回 s
中 唯一 的 p
的 非空子串 的数量 。
//
//
//
//示例 1:
//
//
//输入: p = "a"
//输出: 1
//解释: 字符串 s 中只有一个"a"子字符。
//
//
//示例 2:
//
//
//输入: p = "cac"
//输出: 2
//解释: 字符串 s 中的字符串“cac”只有两个子串“a”、“c”。.
//
//
//示例 3:
//
//
//输入: p = "zab"
//输出: 6
//解释: 在字符串 s 中有六个子串“z”、“a”、“b”、“za”、“ab”、“zab”。
//
//
//
//
//提示:
//
//
// 1 <= p.length <= 105
// p
由小写英文字母构成
//
//
👍 267👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.HashSet;
// 467:环绕字符串中唯一的子字符串
public class UniqueSubstringsInWraparoundString {
public static void main(String[] args) {
Solution solution = new UniqueSubstringsInWraparoundString().new Solution();
// TO TEST
System.out.println(solution.findSubstringInWraproundString("zaba"));
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findSubstringInWraproundString(String p) {
int[] dp = new int[26];
int count = 1;
dp[p.charAt(0) - 'a'] = 1;
for (int i = 1; i < p.length(); i++) {
int sub = p.charAt(i) - p.charAt(i - 1);
if (sub == 1 || sub == -25) {
count++;
} else {
count = 1;
}
dp[p.charAt(i)-'a']=Math.max(dp[p.charAt(i)-'a'],count);
}
return Arrays.stream(dp).sum();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}