当前位置:网站首页>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
边栏推荐
- 通过流式数据集成实现数据价值(4)-流数据管道
- 自定义登录失败处理
- Arm debugging (1): two methods to redirect printf to serial port in keil
- Juc并发编程09——Condition实现源码分析
- A concise course of fast Fourier transform FFT
- 一文看懂 LSTM(Long Short-Term Memory)
- [untitled]
- Turn: Maugham: reading is a portable refuge
- 杰理之更准确地确定异常地址【篇】
- 通过流式数据集成实现数据价值(5)- 流分析
猜你喜欢

failureForwardUrl与failureUrl

深度选择器

从知识传播的维度对比分析元宇宙

Sim Api User Guide(5)

Arm debugging (1): two methods to redirect printf to serial port in keil

101. Symmetric Tree

2022 mobile crane driver test question bank simulation test platform operation

JUC concurrent programming 09 -- source code analysis of condition implementation

構建元宇宙時代敏捷制造的九種能力

Configuration of LNMP
随机推荐
SQL tuning series - SQL performance methodology
最长公共前串
ansible playbook语法和格式 自动化云计算
2022年流动式起重机司机考试题库模拟考试平台操作
DBA常用SQL语句(3)- cache、undo、索引和等待事件
DBA常用SQL语句(4)- Top SQL
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
DBA常用SQL语句(2)— SGA和PGA
formatTime时间戳格式转换
Common DBA SQL statements (4) - Top SQL
杰理之有时候发现内存被篡改,但是没有造成异常,应该如何查找?【篇】
利用多线程按顺序连续输出abc10次
A concise course of fast Fourier transform FFT
Ansible cloud computing automation command line compact version
0704、ansible----01
ansible 云计算 自动化 命令行精简版
[2020wc Day2] F. Clarice picking mushrooms (subtree and query, light and heavy son thought)
Shell script interaction free
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
Custom login failure handling