当前位置:网站首页>Leetcode simple question: check whether two strings are almost equal
Leetcode simple question: check whether two strings are almost equal
2022-07-06 15:04:00 【·Starry Sea】
subject
If two strings word1 and word2 In the from ‘a’ To ‘z’ The difference in the frequency of each letter is No more than 3 , So we call these two strings word1 and word2 Almost equal .
Here you are. Both lengths are n String word1 and word2 , If word1 and word2 Almost equal , Please return true , Otherwise return to false .
A letter x Appearance frequency It refers to the number of times it appears in the string .
Example 1:
Input :word1 = “aaaa”, word2 = “bccb”
Output :false
explain : character string “aaaa” There is 4 individual ‘a’ , however “bccb” There is 0 individual ‘a’ .
The difference between the two is 4 , Greater than upper limit 3 .
Example 2:
Input :word1 = “abcdeef”, word2 = “abaaacc”
Output :true
explain :word1 and word2 The difference in the frequency of each letter in the is at most 3 :
- ‘a’ stay word1 In the 1 Time , stay word2 In the 4 Time , The difference is 3 .
- ‘b’ stay word1 In the 1 Time , stay word2 In the 1 Time , The difference is 0 .
- ‘c’ stay word1 In the 1 Time , stay word2 In the 2 Time , The difference is 1 .
- ‘d’ stay word1 In the 1 Time , stay word2 In the 0 Time , The difference is 1 .
- ‘e’ stay word1 In the 2 Time , stay word2 In the 0 Time , The difference is 2 .
- ‘f’ stay word1 In the 1 Time , stay word2 In the 0 Time , The difference is 1 .
Example 3:
Input :word1 = “cccddabba”, word2 = “babababab”
Output :true
explain :word1 and word2 The difference in the frequency of each letter in the is at most 3 : - ‘a’ stay word1 In the 2 Time , stay word2 In the 4 Time , The difference is 2 .
- ‘b’ stay word1 In the 2 Time , stay word2 In the 5 Time , The difference is 3 .
- ‘c’ stay word1 In the 3 Time , stay word2 In the 0 Time , The difference is 3 .
- ‘d’ stay word1 In the 2 Time , stay word2 In the 0 Time , The difference is 2 .
Tips :
n == word1.length == word2.length
1 <= n <= 100
word1 and word2 All contain only lower case letters .
source : Power button (LeetCode)
Their thinking
The almost equal strings given in the title are based on whether the frequency of characters appears differently , So the first step is to count the frequency of each character in each string , Then compare them one by one .
class Solution:
def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
word1=Counter(word1)
word2=Counter(word2)
for i in word1.keys()|word2.keys(): # In string 1 Or string 2 All the letters that have appeared
if abs(word1[i]-word2[i])>3:
return False
return True
边栏推荐
- 函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
- Want to learn how to get started and learn software testing? I'll give you a good chat today
- 5 minutes to master machine learning iris logical regression classification
- ucore lab1 系统软件启动过程 实验报告
- Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
- Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
- [issue 18] share a Netease go experience
- C language do while loop classic Level 2 questions
- China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
- To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
猜你喜欢
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
[HCIA continuous update] advanced features of routing
5分钟掌握机器学习鸢尾花逻辑回归分类
CSAPP家庭作业答案7 8 9章
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
Report on the double computer experiment of scoring system based on 485 bus
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
随机推荐
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
数字电路基础(二)逻辑代数
Quaternion -- basic concepts (Reprint)
ucore lab2 物理内存管理 实验报告
【指针】查找最大的字符串
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Four methods of exchanging the values of a and B
Want to learn how to get started and learn software testing? I'll give you a good chat today
{1,2,3,2,5} duplicate checking problem
MySQL development - advanced query - take a good look at how it suits you
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
JDBC 的四种连接方式 直接上代码
ucore lab8 文件系统 实验报告
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
[HCIA continuous update] working principle of static route and default route
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions