LeetCode 1876: Substrings of Size Three with Distinct Characters Solution
Master LeetCode problem 1876 (Substrings of Size Three with Distinct Characters), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1876. Substrings of Size Three with Distinct Characters
Problem Explanation
Explanation:
- We can iterate through the string
sand check each substring of size 3. - For each substring, we can check if it has distinct characters or not.
- If it has distinct characters, we increment a counter.
- Finally, we return the counter as the result.
Time Complexity: O(n) Space Complexity: O(1)
Solution Code
class Solution {
public int countGoodSubstrings(String s) {
int count = 0;
for (int i = 0; i <= s.length() - 3; 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;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1876 (Substrings of Size Three with Distinct Characters)?
This page provides optimized solutions for LeetCode problem 1876 (Substrings of Size Three with Distinct Characters) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1876 (Substrings of Size Three with Distinct Characters)?
The time complexity for LeetCode 1876 (Substrings of Size Three with Distinct Characters) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1876 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1876 in Java, C++, or Python.