当前位置:网站首页>leetcode:242. Valid Letter ectopic words
leetcode:242. Valid Letter ectopic words
2022-06-23 13:42:00 【uncle_ ll】
242. Effective alphabetic words
source : Power button (LeetCode)
link : https://leetcode.cn/problems/valid-anagram/
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 ∗ 1 0 4 5 * 10^4 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 ?
solution
- Sort : The two are sorted and then compared element by element ;
- Hashtable : First, judge whether the length of the two is the same , Return directly if the length is different false; In addition, a hash table is used to traverse the structure of comparison storage ,key Is an element ,value Is the number of times the element appears ; Then iterate over another string , If the element is in the hash table , Reduce the number of times 1, If you reduce it to a negative number , Go straight back to false, If not in the hash table , And go straight back to false; Traverse to the end , Look at each... In the hash table key Whether the corresponding times are 0;
Code implementation
Sort
Use the properties of the list ;
python Realization
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return sorted(s) == sorted(t)
c++ Realization
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size())
return false;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
for (int i=0; i<s.size(); i++) {
if (s[i] != t[i])
return false;
}
return true;
}
};
Complexity analysis
- Time complexity : O ( N l o g N ) O(NlogN) O(NlogN)
- Spatial complexity : O ( l o g N ) O(logN) O(logN) Sorting requires space
Hashtable
python Realization
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
counts = dict()
for ch in s:
if ch in counts:
counts[ch] += 1
else:
counts[ch] = 1
for ch in t:
if ch in counts:
counts[ch] -= 1
if counts[ch] == 0:
del counts[ch] # The deletion times are 0 Of key
else:
return False
return len(counts) == 0
c++ Realization
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size())
return false;
unordered_map<char, int> counts;
for(char ch: s) {
auto it = counts.find(ch);
if (it != counts.end())
counts[ch]++;
else
counts[ch] = 1;
}
for (char ch: t) {
auto it = counts.find(ch);
if (it == counts.end())
return false;
counts[ch]--;
if (counts[ch] == 0)
counts.erase(ch);
}
return counts.size() == 0;
}
};
Complexity analysis
- Time complexity : O ( N ) O(N) O(N)
- Spatial complexity : O ( S ) O(S) O(S) The number of characters
边栏推荐
- Germancreditdata of dataset: a detailed introduction to the introduction, download and use of germancreditdata dataset
- Scope of groovy
- The filter function of dplyr package in R language filters the data rows containing the specified string in the specified data column of dataframe data based on the grepl function
- The redis keys command should be used with caution in the production environment. It is best to shield it
- Tuikit audio and video low code solution navigation page
- 2022 年以后,对AI开发人员意味着什么
- "Four highs" of data midrange stability | startdt Tech Lab 18
- Getting started with reverse debugging - learn about PE structure files
- What is the reason why maxcompute is sometimes particularly slow to execute SQL queries
- POW共识机制
猜你喜欢

windows 安装 MySQL

Hanyuan high tech USB2.0 optical transceiver USB2.0 optical fiber extender USB2.0 optical fiber transmitter USB2.0 interface to optical fiber

Quartus II 13.1 安装步骤详解

华三交换机配置SSH远程登录

逆向调试入门-了解PE结构文件

C语言的基本数据类型及其打印输出

边缘和物联网学术资源

How deci and Intel can achieve up to 16.8x throughput improvement and +1.74% accuracy improvement on mlperf

首次曝光!唯一全域最高等级背后的阿里云云原生安全全景图

How did Tencent's technology bulls complete the overall cloud launch?
随机推荐
何小鹏:如果可以回到创业的时候 不会以自己的名字给产品命名
互联网技术发展内卷后的出路——iVX的诞生
Yyds dry inventory solution sword finger offer: judge whether it is a balanced binary tree
How long is the financial product? Is it better for novices to buy long-term or short-term?
理解ADT与OOP
js: 获取页面最大的zIndex(z-index)值
如何正确计算导入Excel的行数(POI/NPOI)
2 万字 + 30 张图 |MySQL 日志:undo log、redo log、binlog 有什么用?
You call this shit MQ?
Strengthen the sense of responsibility and bottom line thinking to build a "safety dike" for flood fighting and rescue
KDD 2022 | epileptic wave prediction based on hierarchical graph diffusion learning
PHP handwriting a perfect daemon
Go写文件的权限 WriteFile(filename, data, 0644)?
4E1 PDH optical transceiver 19 inch rack type single fiber transmission 20km E1 interface optical network optical transceiver
React query tutorial ④ - cache status and debugging tools
Hanyuan high tech USB2.0 optical transceiver USB2.0 optical fiber extender USB2.0 optical fiber transmitter USB2.0 interface to optical fiber
【深入理解TcaplusDB技术】单据受理之事务执行
在線文本過濾小於指定長度工具
How to write vite plug-ins
The R language inputs the distance matrix to the hclust function for hierarchical clustering analysis, uses the cutree function to divide the hierarchical clustering clusters, specifies the number of