当前位置:网站首页>哈希——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;
}
}
边栏推荐
- Kubernetes Foundation
- [golang] before method of time type in golang
- Fiddler packet capture tool summary
- 基于NoCode构建简历编辑器
- 如何给自己的网站接入在线客服系统代码
- Paging query of employee information of black maredge takeout
- 【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
- Blue Bridge Cup - binary conversion exercise
- Easy to understand ES6 (IV): template string
- 自学软件测试天赋异禀——不是盖的
猜你喜欢

【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)

Text message verification of web crawler

Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?

This should be postman, the most complete interface testing tool in the whole network

Redis 100 million level data storage scheme hash slot partition

Ask n! How many zeros are there behind

Nodejs ctf 基础

Hcip OSPF interface network type experiment day 4

tcp 服务端接收数据处理思路梳理,以及select: Invalid argument报错 笔记

Semaphore详解
随机推荐
Exceptions about configuring Postgres parameters
MySQL paging
【Golang】golang实现发送微信服务号模板消息
JMeter runtime controller
[golang] golang realizes sending wechat service number template messages
Leetcode 257. all paths of binary tree
Jmeter-If控制器
Fiddler packet capture tool summary
16 tips for system administrators to use iptables
Semaphore details
性能测试总结(一)---基础理论篇
《Nature》论文插图复刻第3期—面积图(Part2-100)
系统管理员需知的 16 个 iptables 使用技巧
Reprint of illustrations in nature, issue 3 - area map (part2-100)
【C】 Recursive and non recursive writing of binary tree traversal
2022, the average salary of the soft tester, after reading it, I was instantly cool
Win10 icon turns white, recovery method
Logic of automatic reasoning 06 -- predicate calculus
Stream stream
Build resume editor based on Nocode