Substrings of Size Three with Distinct Characters
问长度为3的substring, 是不是都是不同字符的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public int countGoodSubstrings(String s) { int n = s.length(); if(n < 3) return 0; int count = 0; for(int i = 0; i < n - 2; i++) { if(s.charAt(i) != s.charAt(i + 1) && s.charAt(i) != s.charAt(i + 2) && s.charAt(i + 1) != s.charAt(i + 2)) count ++; } return count; } } |