LeetCode | 1358. Number of Substrings Containing All Three Characters包含所有三种字符的子字符串数目【Python】

LeetCode 1358. Number of Substrings Containing All Three Characters包含所有三种字符的子字符串数目【Medium】【Python】【双指针】【滑窗】

Problem

LeetCode

Given a string s consisting only of characters a, b and c.

Return the number of substrings containing at least one occurrence of all these characters a, b and c.

Example 1:

Input: s = "abcabc"
Output: 10
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again). 

Example 2:

Input: s = "aaacb"
Output: 3
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb". 

Example 3:

Input: s = "abc"
Output: 1

Constraints:

  • 3 <= s.length <= 5 x 10^4
  • s only consists of a, b or c characters.

问题

力扣

给你一个字符串 s ,它只包含三种字符 a, b 和 c 。

请你返回 a,b 和 c 都 至少 出现过一次的子字符串数目。

示例 1:

输入:s = "abcabc"
输出:10
解释:包含 a,b 和 c 各至少一次的子字符串为 "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" 和 "abc" (相同字符串算多次)。

示例 2:

输入:s = "aaacb"
输出:3
解释:包含 a,b 和 c 各至少一次的子字符串为 "aaacb", "aacb" 和 "acb" 。

示例 3:

输入:s = "abc"
输出:1

提示:

  • 3 <= s.length <= 5 x 10^4
  • s 只包含字符 a,b 和 c 。

思路

双指针 滑动窗口

1. 如果窗口内有 a, b, c, 那么窗口继续向右拉开都满足
2. 如果窗口内没有 a, b, c, 那么 right 指针右移找到满足窗口内有 a,  b, c
3. 然后 left 指针右移,每次都要判断上面两种情况

时间复杂度: O(n)
空间复杂度: O(n)

Python3代码

class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        if len(s) <= 2:
            return 0
        length = len(s)
        left, right = 0, 2
        ans = 0
        while left < length - 2:
            window = s[left: right + 1]  # [left, right + 1)
            if 'a' in window and 'b' in window and 'c' in window:
                ans += length - right  # if s[left: right + 1] satisfies, then s[left: length] also satisfies
                left += 1  # move left
            else:
                right += 1  # move right
                if right == length:  # s[left: length] does not satisfy, so s[left + x: length] also does not satisfy
                    break
        return ans

代码地址

GitHub链接

原文地址:https://www.cnblogs.com/wonz/p/12348678.html

时间: 2024-08-02 10:11:35

LeetCode | 1358. Number of Substrings Containing All Three Characters包含所有三种字符的子字符串数目【Python】的相关文章

[LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[leetcode]_Palindrome Number

判断integer是否为回文串(负数全部不为回文串) 思路很直接,提取出integer中的每一位,一头一尾进行比较是否相同. 一次AC , 直接上代码: public boolean isPalindrome(int x) { if(x < 0) return false; else if(x >= 0 && x <= 9) return true; else{ int[] num = new int[20]; int i = 0 ; while(x > 0){ n

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

LeetCode——Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

[LeetCode] Palindrome Number [13]

题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

Leetcode Happy Number 弗洛伊德判环解循环

今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表. Leetcode Happy Number 就是这样一道简单的题,实现方法有很多,但是弗洛伊德判环是比较简单,同时效率也是较高的那种. class Solution { public: int change(int n){ int ans = 0; for ( ; n!=0 ; ans+= (n