当前位置:网站首页>Leetcode question brushing: hash table 01 (valid Letter ectopic words)
Leetcode question brushing: hash table 01 (valid Letter ectopic words)
2022-06-23 18:25:00 【Taotao can't learn English】
242. Effective alphabetic words
Given two strings s and t , Write a function to determine t Whether it is s Letter heteronym of .
Example 1:
Input : s = “anagram”, t = “nagaram”
Output : true
Example 2:
Input : s = “rat”, t = “car”
Output : false
explain :
You can assume that the string contains only lowercase letters .
The first idea to get this question is to divide each string into character arrays , two for Circular bubble sort , Last bit by bit comparison
/** * 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 . * source : Power button (LeetCode) * link :https://leetcode.cn/problems/valid-anagram * 1 <= s.length, t.length <= 5 * 10^4 * s and t Only lowercase letters * * @param s * @param t * @return */
public static boolean isAnagram(String s, String t) {
// If the length is different , Then it must not be equal
if (s.length() != t.length()) {
return false;
}
// Both strings are split into character arrays , Then sort , Compare them bit by bit
char[] arrS = s.toCharArray();
char[] arrT = t.toCharArray();
for (int i = 0; i < arrS.length - 1; i++) {
for (int j = i + 1; j < arrS.length; j++) {
if (arrS[i] > arrS[j]) {
char temp = arrS[i];
arrS[i] = arrS[j];
arrS[j] = temp;
}
if (arrT[i] > arrT[j]) {
char temp = arrT[i];
arrT[i] = arrT[j];
arrT[j] = temp;
}
}
}
for (int i = 0; i <arrS.length ; i++) {
if(arrS[i]!=arrT[i]) {
return false;
}
}
return true;
}
The result is obviously undesirable , Although the idea is right , But overtime
Change the way of thinking , Altogether 26 Lowercase letters , It is better to use an array to record every occurrence at this time
/** * 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 . * source : Power button (LeetCode) * link :https://leetcode.cn/problems/valid-anagram * 1 <= s.length, t.length <= 5 * 10^4 * s and t Only lowercase letters * * @param s * @param t * @return */
public static boolean isAnagram(String s, String t) {
// If the length is different , Then it must not be equal
if (s.length() != t.length()) {
return false;
}
// Create an alphabet with matching digits
char[] arrS = s.toCharArray();
char[] arrT = t.toCharArray();
int[] numS = new int[26];
int[] numT = new int[26];
for (int i = 0; i < arrS.length; i++) {
numS[arrS[i] % 26] += 1;
numT[arrT[i] % 26] += 1;
}
for (int i = 0; i < 26; i++) {
if(numS[i]!=numT[i]) {
return false;
}
}
return true;
}

The effect is much better . Look at the solution , It is similar to me .
class Solution {
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;
}
for (int i : record) {
if (i != 0) {
return false;
}
}
return true;
}
}
It's just that the solution uses a counting array , Add... For the first time 1, Second time subtraction 1. About the same .
边栏推荐
- 【华中科技大学】考研初试复试资料分享
- 论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure
- Customer service system building tutorial_ Installation and use mode under the pagoda panel_ Docking with official account_ Support app/h5 multi tenant operation
- Wiley-中国科学院文献情报中心开放科学联合研讨会第二讲:开放获取期刊选择及论文投稿...
- Latex使用\usepackage{hyperref}报错:paragraph ended before [email protected]@link was complete
- Video anomaly detection data set (shanghaitech)
- Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
- 论文阅读 (49):Big Data Security and Privacy Protection (科普文)
- 论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
- 【ESP8266-01s】获取天气,城市,北京时间
猜你喜欢

Paper reading (48):a Library of optimization algorithms for organizational design

QT实现基于规则的机器翻译系统 课程论文+任务书+项目源码

Stepping mode of research control motor

CSDN salary increase secret script: Jenkins integrated allure test report complete tutorial

Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints

leetcode刷题:哈希表01 (有效的字母异位词)

Paper reading (54):deepfool: a simple and accurate method to four deep neural networks

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...

科技互动沙盘是凭借什么收获人气的

微信小程序startLocationUpdateBackground()简单实现骑手配送位置
随机推荐
Redis 集群
MySQL -- classic interview questions
TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
Strong cache and negotiation cache in http
leetcode刷题:哈希表03 (快乐数)
全局组织结构控制之抢滩登陆
Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
聊一聊数据库的行存与列存
Leetcode: hash table 07 (sum of three numbers)
实现领域驱动设计 - 使用ABP框架 - 通用准则
Redis cluster
Implementing Domain Driven Design - using ABP framework - repository
Regular expression use graph bed
【win10 VS2019 opencv4.6 配置参考】
论文阅读 (56):Mutli-features Predction of Protein Translational Modification Sites (任务)
正则表达式使用图床
剑指 Offer II 092. 翻转字符 / 剑指 Offer II 093. 最长斐波那契数列
Is it safe to open a stock account online in 2022?
论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
[learning notes] tidb learning notes (III)
