当前位置:网站首页>242、有效字母异位词(哈希表)
242、有效字母异位词(哈希表)
2022-04-23 10:11:00 【Popuessing's Jersey】
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1: 输入: s = "anagram", t = "nagaram" 输出: true
示例 2: 输入: s = "rat", t = "car" 输出: false
假设字符串只包含小写字母。
public boolean isAnagram(String s,String t){
//新建一个数组记录每个字符出现次数
int [] record = new int [26];
//遍历字符串,如果字符出现一次,出现值加一
for (char c:s.toCharArray()) {
record[c-'a']+=1;
}
for (char c:t.toCharArray()) {
record[c-'a']-=1;
}
//如果数组中的每个值都为0,那么两个字符串就是字母异位词
for (int x:record) {
if (x!=0){
return false;
}
}
return true;
}
public static void main(String[] args) {
String s = "faker";
String t = "kafer";
Youxuaidezunyyiweici youxuaidezunyyiweici = new Youxuaidezunyyiweici();
boolean b = youxuaidezunyyiweici.isAnagram(s,t);
System.out.println(b);
}
输出结果:
True
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121474870
边栏推荐
- 2022年上海市安全员C证考试题库及答案
- Realizing data value through streaming data integration (5) - stream processing
- LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
- Career planning and implementation in the era of meta universe
- Juc并发编程09——Condition实现源码分析
- Using multithreading to output abc10 times in sequence
- 工业元宇宙平台规划与建设
- [untitled]
- JUC concurrent programming 09 -- source code analysis of condition implementation
- [untitled]
猜你喜欢
Windows安装redis并将redis设置成服务开机自启
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
[untitled]
Arm debugging (1): two methods to redirect printf to serial port in keil
JUC concurrent programming 09 -- source code analysis of condition implementation
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
Depth selector
Sim Api User Guide(6)
Zhengda international explains what the Dow Jones industrial index is?
深度选择器
随机推荐
Solve the problem of installing VMware after uninstalling
Introduction to graph theory -- drawing
通过流式数据集成实现数据价值(2)
Sim Api User Guide(7)
Sim Api User Guide(8)
[COCI] Vje š TICA (subset DP)
Sim Api User Guide(6)
Chapter II in memory architecture (im-2.2)
SQL调优系列文章之—SQL性能方法论
Nvidia最新三维重建技术Instant-ngp初探
shell脚本免交互
元宇宙时代的职业规划与执行
DBA common SQL statements (1) - overview information
【无标题】
杰理之更准确地确定异常地址【篇】
Computer network security experiment II DNS protocol vulnerability utilization experiment
Pyqt5与通信
0704、ansible----01
Art template template engine
从知识传播的维度对比分析元宇宙