当前位置:网站首页>A substring with a length of three and different characters in the leetcode simple question
A substring with a length of three and different characters in the leetcode simple question
2022-07-05 22:03:00 【·Starry Sea】
subject
If a string does not contain any duplicate characters , We call this string good character string .
Give you a string s , Please return s The middle length is 3 Of Good substring The number of .
Be careful , If the same good substring appears more than once , Every time should be included in the answer .
Substring Is a continuous sequence of characters in a string .
Example 1:
Input :s = “xyzzaz”
Output :1
explain : All in all 4 A length of 3 Substring of :“xyz”,“yzz”,“zza” and “zaz” .
The only length is 3 The good substring is “xyz” .
Example 2:
Input :s = “aababcabc”
Output :4
explain : All in all 7 A length of 3 Substring of :“aab”,“aba”,“bab”,“abc”,“bca”,“cab” and “abc” .
Good substrings include “abc”,“bca”,“cab” and “abc” .
Tips :
1 <= s.length <= 100
s Only lowercase letters .
source : Power button (LeetCode)
Their thinking
To determine whether there are duplicate elements, you can use a hash table , Then traverse the string and check one by one 3 Are all the elements different .
class Solution:
def countGoodSubstrings(self, s: str) -> int:
if len(s)<3:
return 0
i=2
count=0
while i<len(s):
if len(set(s[i-2:i+1]))==3:
count+=1
i+=1
return count
边栏推荐
- Stored procedures and stored functions
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- Kingbasees v8r3 data security case - audit record clearing case
- Drawing HSV color wheel with MATLAB
- MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
- K210 learning notes (IV) k210 runs multiple models at the same time
- QML reported an error expected token ";", expected a qualified name ID
- Huawei fast game failed to call the login interface, and returned error code -1
- MMAP
- Summary of concurrency control
猜你喜欢
K210学习笔记(四) K210同时运行多个模型
Granularity of blocking of concurrency control
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Blocking protocol for concurrency control
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Oracle hint understanding
华为云ModelArts文本分类–外卖评论
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
随机推荐
Type of fault
NET中小型企业项目开发框架系列(一个)
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
Multiplexing of Oracle control files
EL与JSTL注意事项汇总
A trip to Suzhou during the Dragon Boat Festival holiday
ICMP introduction
Form artifact
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
Poj3414广泛搜索
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
K210 learning notes (IV) k210 runs multiple models at the same time
How to organize an actual attack and defense drill
微服務鏈路風險分析
Regular expressions and re Libraries
Storage optimization of performance tuning methodology
Efficiency difference between row first and column first traversal of mat data types in opencv
Huawei cloud modelarts text classification - takeout comments
Decorator learning 01
[Yugong series] go teaching course 003-ide installation and basic use in July 2022