当前位置:网站首页>Code random notes_ Hash_ 242 effective letter heterotopic words
Code random notes_ Hash_ 242 effective letter heterotopic words
2022-07-27 06:52:00 【Erik_ Won】
Code random notes _ Hashtable
Code Capriccio record two brush notes
LC242. Effective alphabetic words
subject
Given two strings s and t , Write a function to determine t Whether it is s Letter heteronym of .
Be careful : if s and t Each character in the has the same number of occurrences , said s and t They are mutually alphabetic words .
Example 1:
Input : s = “anagram”, t = “nagaram”
Output : true
Example 2:
Input : s = “rat”, t = “car”
Output : false
Thought analysis
because s and t Only lowercase letters ,26 The letters are fixed , You can build an array of records record, take s Record the number of times each character appears in ( With + In the form of ) , After then t Record the number of times each character appears in ( With - In the form of ) .
If it's an alphabetic word , Then the final record array must be all 0, Otherwise, it is not an alphabetic ectopic word .
Code implementation
Complete code implementation
public boolean isAnagram(String s, String t) {
// take s and t Convert to char Array
char[] sca = s.toCharArray();
char[] tca = t.toCharArray();
// Maintain a hash dictionary
int[] dict = new int[26];
// take s Characters in + A formal record of
for(char c:sca){
dict[c - 'a']++;
}
// take t Characters in - A formal record of
for(char c:tca){
dict[c - 'a']--;
}
// If the final dictionary is 0 It represents the letter ectopic word required by the title
for(int i:dict){
if(i != 0) return false;
}
return true;
}
边栏推荐
- PSI | CSI and ROC | AUC and KS - memorandum
- After adding a camera (camera) to the UAV in gazebo, the UAV cannot take off
- 系统安全与应用
- How to avoid loopholes? Sunflower remote explains the safe use methods in different scenarios
- 向日葵教大家如何防范拒绝服务攻击漏洞?
- Socket programming II: using Select
- goLang的一个跨域问题
- 关于在Gazebo中给无人机添加相机(摄像头)之后,无人机无法起飞
- Open source WebGIS related knowledge
- ESXI虚拟机启动,模块“MonitorLoop”打开电源失败
猜你喜欢

win10 添加虚拟网卡,配置op路由

Install redis under Windows

RAID详解与配置

Add virtual network card and configure OP route in win10

Linux安装Redis操作

Redis fast learning

Use of getattr, hasattr, delattr and setattr in reflectors

云原生运行环境搭建

Ftx.us launched stock and ETF trading services to make trading more transparent

2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!
随机推荐
Explanation of server related indicators
DNS domain name resolution service
Redis fast learning
FTX US launched FTX stocks, striding forward to the mainstream financial industry
Tips - completely delete the files on the USB flash drive
Pruning - quantification - turn to onnx Chinese series tutorials
Use of getattr, hasattr, delattr and setattr in reflectors
使用密钥方式登录阿里云服务器
Socket programming II: using Select
向日葵全面科普,为你的远程控制设备及时规避漏洞
Account management and authority
Shell programming specification and redirection and pipeline operation
Shell -- circular statements (for, while, until)
About the problem that Druid can't connect to the database
Disk management and file system
Shell -- custom variables and assignments
Ftx.us launched stock and ETF trading services to make trading more transparent
Go language learning
Memo @restcontrolleradvice and exception interception class example
账号管理与权限