1876:长度为三且各字符不同的子字符串

This commit is contained in:
huangge1199 2021-06-07 13:12:25 +08:00
parent 66aa44dbaa
commit 3421ec0a55
2 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,65 @@
//如果一个字符串不含有任何重复字符我们称这个字符串为 字符串
//
// 给你一个字符串 s 请你返回 s 中长度为 3 好子字符串 的数量
//
// 注意如果相同的好子字符串出现多次每一次都应该被记入答案之中
//
// 子字符串 是一个字符串中连续的字符序列
//
//
//
// 示例 1
//
//
//输入s = "xyzzaz"
//输出1
//解释总共有 4 个长度为 3 的子字符串"xyz""yzz""zza" "zaz"
//唯一的长度为 3 的好子字符串是 "xyz"
//
//
// 示例 2
//
//
//输入s = "aababcabc"
//输出4
//解释总共有 7 个长度为 3 的子字符串"aab""aba""bab""abc""bca""cab" "abc"
//好子字符串包括 "abc""bca""cab" "abc"
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 100
// s 只包含小写英文字母
//
// Related Topics 字符串
// 👍 2 👎 0
package leetcode.editor.cn;
//1876:长度为三且各字符不同的子字符串
public class SubstringsOfSizeThreeWithDistinctCharacters{
public static void main(String[] args) {
//测试代码
Solution solution = new SubstringsOfSizeThreeWithDistinctCharacters().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int countGoodSubstrings(String s) {
if(s.length()<3){
return 0;
}
int count = 0;
for (int i = 1; i < s.length() - 1; i++) {
if(s.charAt(i-1)!=s.charAt(i)&&s.charAt(i-1)!=s.charAt(i+1)&&s.charAt(i+1)!=s.charAt(i)){
count++;
}
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,37 @@
<p>如果一个字符串不含有任何重复字符,我们称这个字符串为 <strong></strong> 字符串。</p>
<p>给你一个字符串 <code>s</code> ,请你返回 <code>s</code> 中长度为 <strong>3</strong> 的 <strong>好子字符串</strong> 的数量。</p>
<p>注意,如果相同的好子字符串出现多次,每一次都应该被记入答案之中。</p>
<p><strong>子字符串</strong> 是一个字符串中连续的字符序列。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>s = "xyzzaz"
<b>输出:</b>1
<b>解释:</b>总共有 4 个长度为 3 的子字符串:"xyz""yzz""zza" 和 "zaz" 。
唯一的长度为 3 的好子字符串是 "xyz" 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>s = "aababcabc"
<b>输出:</b>4
<b>解释:</b>总共有 7 个长度为 3 的子字符串:"aab""aba""bab""abc""bca""cab" 和 "abc" 。
好子字符串包括 "abc""bca""cab" 和 "abc" 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> 只包含小写英文字母。</li>
</ul>
<div><div>Related Topics</div><div><li>字符串</li></div></div>\n<div><li>👍 2</li><li>👎 0</li></div>