当前位置:网站首页>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;
}
边栏推荐
- 云原生运行环境搭建
- 如何避免漏洞?向日葵远程为你讲解不同场景下的安全使用方法
- Express接收请求参数
- Use -wall to clear code hidden dangers
- Raid explanation and configuration
- Three methods to judge whether it is palindrome structure
- Lamp -- source code compilation and installation
- 2022年全球最具技术实力的的智能合约安全审计公司推荐
- Webodm win10 installation tutorial (personal test)
- Redis operation of Linux Installation
猜你喜欢

Project training experience 2

FTX 基金会资助1500万帮助新冠疫苗临床实验,将影响全球公共卫生

智能安防视频平台EasyCVR出现通道列表为空情况的原因是什么?

EasyCVR设备管理列表页面搜索时,分页数据不显示的问题修复

多模态数据库 | 星环科技多模数据库ArgoDB“一库多用“,构建高性能湖仓集一体平台

QGIS series (1) -qgis (server APACHE) win10 installation

What if the website server is attacked? Sunflower tips that preventing loopholes is the key

Multimodal database | star ring technology multimode database argodb "one database for multiple purposes", building a high-performance Lake warehouse integrated platform

MangoDB

Disk management and file system
随机推荐
向日葵教大家如何防范拒绝服务攻击漏洞?
How to manage a large number of scheduled tasks
3D打印品牌的康复骨科支具有何特别之处?
备忘录 @RestControllerAdvice与异常拦截类示例
DHCP的概念和原理
Socket programming II: using Select
gin-vue-admin 使用docker容器中的数据库
账号管理与权限
QGIS series (1) -qgis (server APACHE) win10 installation
Use -wall to clear code hidden dangers
PXE efficient batch network installation
自己动手实现容器
A cross domain problem of golang
RAID详解与配置
DNS domain name resolution service
如何避免漏洞?向日葵远程为你讲解不同场景下的安全使用方法
LVM与磁盘配额
pycharm在虚拟环境下跑jupyter notebook问题记录
ES6的新特性(2)
Disk management and file system