Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 1.47 KB

_1358. Number of Substrings Containing All Three Characters.md

File metadata and controls

62 lines (46 loc) · 1.47 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : March 11, 2025

Last updated : March 11, 2025


Related Topics : Hash Table, String, Sliding Window

Acceptance Rate : 73.01 %


Solutions

Python

class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        abc = [-1, -1, -1]
        output = 0

        for i, x in enumerate(s) :
            abc[ord(x) - ord('a')] = i
            if -1 not in abc :
                output += 1 + min(abc)

        return output
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        a = b = c = -1
        output = 0

        for i, x in enumerate(s) :
            match x :
                case 'a' :
                    a = i
                case 'b' :
                    b = i
                case 'c' :
                    c = i

            if min(a, b, c) != -1 :
                output += 1 + min(a, b, c)

        return output