当前位置:网站首页>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
边栏推荐
- How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of BBI, MTM, obv, CCI and priceosc indicators
- MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
- [JS] battle chess
- [Latex] 插入图片
- [JS component] calendar
- Cards are unpredictable
- Biological unlocking - Fingerprint entry process
- [JS component] create a custom horizontal and vertical scroll bar following the steam style
- Go simple read database
- Rest at home today
猜你喜欢
论文笔记:STMARL: A Spatio-Temporal Multi-AgentReinforcement Learning Approach for Cooperative Traffic
Unity calls alertdialog
Jenkins持续集成操作
What is the difference between pytorch and tensorflow?
The scope builder coroutinescope, runblocking and supervisorscope of kotlin collaboration processes run synchronously. How can other collaboration processes not be suspended when the collaboration pro
[JS component] calendar
[JS component] customize the right-click menu
[JS component] custom paging
Pipeline流水线项目构建
Alexnet实现Caltech101数据集图像分类(pytorch实现)
随机推荐
Minimum spanning tree problem
Unity extension
Unitywebrequest asynchronous Download
ROS从入门到精通(零) 教程导读
Leetcode-11- container with the most water (medium)
Traditional machine learning classification model predicts the rise and fall of stock prices
How many steps are appropriate for each cycle of deep learning?
Self use notes for problem brushing learning
MySQL异常:com.mysql.jdbc.PacketTooBigException: Packet for query is too large(4223215 > 4194304)
[server data recovery] successful cases of data loss recovery during data migration between storage servers
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
Tree - delete all leaf nodes
Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
Illustrator tutorial, how to add dashes and arrows in illustrator?
Android Weather
[Latex] 插入圖片
What is pytorch? Explain the basic concepts of pytorch
Binary tree -- using hierarchical sequence and middle sequence to determine a tree
Matrix fast power
[JS component] create a custom horizontal and vertical scroll bar following the steam style