当前位置:网站首页>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
边栏推荐
- [pointer] octal to decimal
- 数字电路基础(五)算术运算电路
- 【指针】求字符串的长度
- c语言学习总结(上)(更新中)
- Stc-b learning board buzzer plays music 2.0
- [pointer] delete all spaces in the string s
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
- 指针--剔除字符串中的所有数字
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- "If life is just like the first sight" -- risc-v
猜你喜欢
Query method of database multi table link
Fundamentals of digital circuits (I) number system and code system
Réponses aux devoirs du csapp 7 8 9
Interview Essentials: what is the mysterious framework asking?
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
Install and run tensorflow object detection API video object recognition system of Google open source
MySQL development - advanced query - take a good look at how it suits you
随机推荐
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
[HCIA continuous update] working principle of static route and default route
How to solve the poor sound quality of Vos?
王爽汇编语言详细学习笔记二:寄存器
Function: string storage in reverse order
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
【指针】统计一字符串在另一个字符串中出现的次数
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Pointeurs: maximum, minimum et moyenne
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
指針:最大值、最小值和平均值
5分钟掌握机器学习鸢尾花逻辑回归分类
How to transform functional testing into automated testing?
Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
[oiclass] maximum formula
数字电路基础(一)数制与码制
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share