当前位置:网站首页>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
边栏推荐
- HDU 4391 paint the wall segment tree (water
- 每日刷题记录 (十四)
- The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
- HYSBZ 2243 染色 (树链拆分)
- 他们主动布局(autolayout)环境的图像编辑器
- Meituan dynamic thread pool practice ideas, open source
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Analyse des risques liés aux liaisons de microservices
- Daily question brushing record (XIV)
- Pl/sql basic syntax
猜你喜欢
Analysis and test of ModbusRTU communication protocol
Index optimization of performance tuning methodology
Server optimization of performance tuning methodology
华为云ModelArts文本分类–外卖评论
How to use tensorflow2 for cat and dog classification and recognition
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Installation of VMware Workstation
Storage optimization of performance tuning methodology
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
随机推荐
The real situation of programmers
Storage optimization of performance tuning methodology
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
阿龙的感悟
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
Detailed explanation of memset() function usage
Common interview questions of JVM manufacturers
总结出现2xx、3xx、4xx、5xx状态码的原因
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
HDU 4391 paint the wall segment tree (water
Ad637 notes d'utilisation
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
初级软件测试必问面试题
Business learning of mall commodity module
华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
HDU 4391 Paint The Wall 段树(水
他们主动布局(autolayout)环境的图像编辑器
Common interview questions of redis factory
Overview of database recovery
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes