当前位置:网站首页>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

边栏推荐
- Four methods of exchanging the values of a and B
- Database monitoring SQL execution
- 【指针】求解最后留下的人
- Wang Shuang's detailed notes on assembly language learning I: basic knowledge
- Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
- 数字电路基础(四) 数据分配器、数据选择器和数值比较器
- 【指针】使用插入排序法将n个数从小到大进行排列
- Pointeurs: maximum, minimum et moyenne
- 指針:最大值、最小值和平均值
- Logstack introduction and deployment -- elasticstack (elk) work notes 019
猜你喜欢

Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary

Keil5-MDK的格式化代码工具及添加快捷方式

后台登录系统,JDBC连接数据库,做小案例练习

About the garbled code problem of superstar script

基于485总线的评分系统双机实验报告

STC-B学习板蜂鸣器播放音乐

5 minutes to master machine learning iris logical regression classification

Fundamentals of digital circuits (II) logic algebra

150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview

四元数---基本概念(转载)
随机推荐
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
JDBC 的四种连接方式 直接上代码
C language learning summary (I) (under update)
Pointeurs: maximum, minimum et moyenne
数字电路基础(一)数制与码制
STC-B学习板蜂鸣器播放音乐2.0
[pointer] the array is stored in reverse order and output
How to learn automated testing in 2022? This article tells you
Why can swing implement a form program by inheriting the JFrame class?
[pointer] find the value of the largest element in the two-dimensional array
Cc36 different subsequences
What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Fundamentals of digital circuit (V) arithmetic operation circuit
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Fundamentals of digital circuits (III) encoder and decoder
About the garbled code problem of superstar script
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
Function: find the root of the equation by Newton iterative method
1.支付系统