当前位置:网站首页>力扣242-有效的字母异位词——哈希表法
力扣242-有效的字母异位词——哈希表法
2022-08-09 04:53:00 【张怼怼√】
题目描述
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。
求解思路
- 创建一个大小为26的数组arr,用于存储每个字母在字符串中出现的次数;
- 首先遍历字符串s,统计s中各个字母的出现次数(次数更新+1);
- 接下来遍历字符串t(次数更新-1);
- 最后看arr中所有元素是否都为0;
- 如果都为0,说明 s 和 t 是字母异位词,返回 true ,反之则返回 false。
输入输出示例

代码
class Solution {
public boolean isAnagram(String s, String t) {
char[] s1 = s.toCharArray();
char[] t1 = t.toCharArray();
int[] arr = new int[26];
for(char c : s1){
arr[c - 'a'] += 1;
}
for(char c : t1){
arr[c - 'a'] -= 1;
}
for(int i : arr){
if(i != 0) return false;
}
return true;
}
}边栏推荐
- Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)
- 无法通过头文件中的宏定义或全局变量修改动态库中的参数
- How to choose an APS system, it is necessary to clarify these seven key factors
- 通讯录(动态版)(C语言)(VS)
- 【Harmony OS】【ARK UI】Custom popup
- perl基础语法归纳
- 【MLT】MLT多媒体框架生产消费架构解析(二)
- 区别如下概念:助记符、汇编语言、汇编语言程序和汇编程序。
- 杰理之SD卡切回蓝牙没有作用【篇】
- 供应商对接Chewy的EDI需求
猜你喜欢

2022年8月深圳产品经理认证招生简章(NPDP)

P1163 银行贷款

杰理之手机OTG问题【篇】

Ali YunTianChi competition problem (machine learning) - O2O coupons prediction (complete code)

How to trim svg and compress

【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed

TCP/IP协议中分包与重组原理介绍、分片偏移量的计算方法、IPv4报文格式

JS-DOM-全局、局部、隐式变量,数组()\函数、 prompt输入对话框、confirm(确定用户的决定-弹出对话框)

【Harmony OS】【FAQ】鸿蒙问题合集1

Faced with risk control, what should Amazon do when evaluating self-supporting accounts?
随机推荐
Harmony OS ets ArkUI 】 【 】 development create a view and building layout
【学习笔记】AGC044
如何选型APS系统,还需明确这七大关键因素
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept
Introduction to JVM garbage collection mechanism
JS-DOM-对象的事件onload、匿名函数、this
【Harmony OS】【ARK UI】Public Event Module
Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)
2022 High-altitude installation, maintenance, and demolition exam practice questions and mock exams
php将在线远程文件写入临时文件
学习笔记--文件夹处理--代码学习
说明高级语言、汇编语言、机器语言三者的区别,谈谈你对汇编语言的认识。
Ali YunTianChi competition problem (deep learning) - video enhancement (complete code)
"IP" command to configure network interface
LN论文、五种归一化原理和实现
杰理之智能充电仓低电发码关机 触摸不开机【篇】
通讯录(文件版)(C语言)(VS)
uboot中board_init bi_arch_number在哪
杰理之电话打入,远端听不到声音【篇】
【暑期每日一题】洛谷 P5724 【深基4.习5】求极差 / 最大跨度值