Hyoseo Lee
01 Index 02 Current page Writing 03 Work 04 Notes 05 Games 06 Author
leetcode

1876. Substrings of Size Three with Distinct Characters

Topic Easy
Area Algorithms

Problem

View on LeetCode →

Difficulty: Easy
Tags: Mid Level, Hash Table, String, Sliding Window, Counting, Biweekly Contest 53

Intuition

This question is much easier than previous question. I could do check all the possible substrings of length 3, and it wasn’t that slow.

if 3 gets bigger to like 10 or 100, I might do with different method, but for this problem, checking every combination was fine.

Approach

let i be idex of first element of substring with length 3, and check every possible substring by increasing i by 1.

Solution

class Solution:
    def countGoodSubstrings(self, s: str) -> int:
        if len(s) < 3:
            return 0

        count = 0
        i = 0
        while i+3 <= len(s):
            if not(s[i] == s[i+1] or s[i] == s[i+2] or s[i+1] == s[i+2]):
                count += 1
            i += 1

        return count

Complexity

  • Time: O(n)O(n)

  • Space: O(1)O(1)

Thoughts

This one was easy.