当前位置:网站首页>leetode. 242. valid Letter heteronyms
leetode. 242. valid Letter heteronyms
2022-06-13 01:03:00 【Didi dada】
- Effective alphabetic words
Given two strings s and t , Write a function to determine t Whether it is s Letter heteronym of .
Be careful : if s and t Each character in the has the same number of occurrences , said s and t They are mutually alphabetic words .
Example 1:
Input : s = “anagram”, t = “nagaram”
Output : true
Example 2:
Input : s = “rat”, t = “car”
Output : false
Tips :
1 <= s.length, t.length <= 5 * 104
s and t Only lowercase letters
Advanced : If the input string contains unicode What about characters ? Can you adjust your solution to deal with this situation ?242. Effective alphabetic words
Given two strings s and t , Write a function to determine t Whether it is s Letter heteronym of .
Be careful : if s and t Each character in the has the same number of occurrences , said s and t They are mutually alphabetic words .
Tips :
1 <= s.length, t.length <= 5 * 104
s and t Only lowercase letters
Advanced : If the input string contains unicode What about characters ? Can you adjust your solution to deal with this situation ?
Method 1 : By judging that the types and times of characters in two strings are equal
class Solution {
public:
bool isAnagram(string s, string t) {
int n1 = s.size();
int n2 = t.size();
if (n1!=n2) return false;
unordered_map<char, int> m;
for(auto it: s){
m[it]++;
}
for(auto it: t){
m[it]--;
if(m[it]==0){
m.erase(it);
}
}
if (m.size()==0) return true;
else return false;
}
};
improvement : When two strings have the same length but are not heterotopic , Or the characters are inconsistent , Or the number of characters is wrong ( There must be a character that appears more times ), For both cases, we are traversing t when , perform m[it]--, Will appear m[it]<0 The situation of , Therefore, the above code can be changed to :
class Solution {
public:
bool isAnagram(string s, string t) {
int n1 = s.size();
int n2 = t.size();
if (n1!=n2) return false;
unordered_map<char, int> m;
for(auto it: s){
m[it]++;
}
for(auto it: t){
m[it]--;
if(m[it]<0){
return false;
}
}
return true;
}
};
Method 2 : Two strings are ectopic , Equivalent to after sorting , Two strings are equal
class Solution:
import collections
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
s_list = list(s)
t_list = list(t)
s_list.sort()
t_list.sort()
return s_list == t_list
边栏推荐
- [imx6ull] video monitoring project (USB camera +ffmepeg)
- Application advantages of 5g industrial gateway in coal industry
- How many steps are appropriate for each cycle of deep learning?
- 软件测试的几种分类,一看就明了
- Androi weather
- [virtual machine] notes on virtual machine environment problems
- 论文笔记:STMARL: A Spatio-Temporal Multi-AgentReinforcement Learning Approach for Cooperative Traffic
- Tree - delete all leaf nodes
- Canvas random bubbling background
- Leetcode-16- sum of the nearest three numbers (medium)
猜你喜欢

Key point detection data preparation and model design based on u-net Network -- detection model of four key points of industrial components

什么是 dummy change?

什么是 Meebits?一个简短的解释
![[Latex] 插入图片](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[Latex] 插入图片

Leetcode-11- container with the most water (medium)

深度学习每周期的步数多少合适?

What is dummy change?

Several categories of software testing are clear at a glance

Binary tree -- using hierarchical sequence and middle sequence to determine a tree

How many steps are appropriate for each cycle of deep learning?
随机推荐
What is pytorch? Explain the basic concepts of pytorch
Android Weather
Common skills for quantitative investment - drawing 2: drawing the moving average
gpu加速pytorch能用吗?
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
Leetcode-16- sum of the nearest three numbers (medium)
Opencv desaturation
深度学习训练多少轮?迭代多少次?
AOF持久化
什么是 dummy change?
[JS component] floating text
Alexnet实现Caltech101数据集图像分类(pytorch实现)
Comparison of disk partition modes (MBR and GPT)
With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
STM32 USB Basics
蓝桥杯单片机第七届决赛
Kotlin collaboration, the life cycle of a job
Remove duplicates from an ordered array
[JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
生物解锁--指纹录入流程