当前位置:网站首页>哈希——242.有效的字母异位词
哈希——242.有效的字母异位词
2022-07-24 11:23:00 【向着百万年薪努力的小赵】
1 题目描述
- 有效的字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。
2 题目示例
示例 1:
输入: s = “anagram”, t = “nagaram”
输出: true
示例 2:
输入: s = “rat”, t = “car”
输出: false
3 题目提示
- 1 <= s.length, t.length <= 5 * 104
- s 和 t 仅包含小写字母
4 思路
方法一:排序
t 是 s 的异位词等价于「两个字符串排序后相等」。因此我们可以对字符串 s 和 t 分别排序,看排序后的字符串是否相等即可判断。此外,如果 s 和 t 的长度不同,t 必然不是 s 的异位词。
复杂度分析
时间复杂度: O(nlogn),其中 n 为 s 的长度。排序的时间复杂度为 O(nlogn),比较两个字符串是否相等时间复杂度为O(n),因此总体时间复杂度为 O(nlogn+n)=O(nlogn)。
空间复杂度:O(logn)。排序需要 O(logn) 的空间复杂度。
方法二:哈希表
从另一个角度考虑,t 是 s 的异位词等价于「两个字符串中字符出现的种类和次数均相等」。由于字符串只包含 26 个小写字母,因此我们可以维护一个长度为 26 的频次数组 table,先遍历记录字符串 s 中字符出现的频次,然后遍历字符串 t,减去 table 中对应的频次,如果出现 table[i]<0,则说明 t 包含一个不在 s 中的额外字符,返回 false 即可。
复杂度分析
时间复杂度:O(n),其中 n 为 s 的长度。
空间复杂度:O(S),其中 S 为字符集大小,此处 S=26。
5 我的答案
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] table = new int[26];
for (int i = 0; i < s.length(); i++) {
table[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
table[t.charAt(i) - 'a']--;
if (table[t.charAt(i) - 'a'] < 0) {
return false;
}
}
return true;
}
}
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
Map<Character, Integer> table = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
table.put(ch, table.getOrDefault(ch, 0) + 1);
}
for (int i = 0; i < t.length(); i++) {
char ch = t.charAt(i);
table.put(ch, table.getOrDefault(ch, 0) - 1);
if (table.get(ch) < 0) {
return false;
}
}
return true;
}
}
边栏推荐
- Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Construction tutorial attached
- Use Modelsim to independently simulate Altera and Xilinx IP cores
- DevOps及DevOps常用的工具介绍
- Semaphore details
- 【Golang】golang实现简单memcache
- Robot framework official tutorial (I) getting started
- Introduction to kubernetes Basics
- Use of getchar
- HDU 3351:Seinfeld
- 【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
猜你喜欢

2022,软测人的平均薪资,看完我瞬间凉了...

JMeter interface test steps - Installation Tutorial - script recording - concurrent test

Redis 100 million level data storage scheme hash slot partition
![About [software testing - interview skills and precautions for automated testing] - talk freely](/img/c2/bd1a52bdd7ab07878348b6216012f0.png)
About [software testing - interview skills and precautions for automated testing] - talk freely

How to convert word to markdown text

IT圈中的Bug的类型与历史

Introduction to kubernetes Basics
![[deserialization vulnerability-01] Introduction to serialization and deserialization](/img/e4/6b9ee6ee74f3cdc3c886ed3af9ef73.png)
[deserialization vulnerability-01] Introduction to serialization and deserialization

Depth first search and breadth first search of Graphs

Kubernetes Foundation
随机推荐
Nodejs CTF Foundation
How to choose sentinel vs. hystrix current limiting?
[deserialization vulnerability-01] Introduction to serialization and deserialization
Xilinx FPGA soft core development process
[golang] golang implements the post request to send form type data function
Leetcode 112. 路径总和
cgo+gSoap+onvif学习总结:9、go和c进行socket通信进行onvif协议处理
Idea runs the wordcount program (detailed steps)
【10】 Teamwork and cross team collaboration
The solution of permission denied
E2PROM read / write (xiicps) on PS side of zcu102 board
【10】团队协作和跨团队协作
Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
Exceptions about configuring Postgres parameters
Stream stream
08【AIO编程】
Stack Title: basic calculator II
PDF处理还收费?不可能!
Directional crawling Taobao product name and price (teacher Songtian)
【Golang】golang实现post请求发送form类型数据函数