当前位置:网站首页>【LeetCode】Day106-单词规律
【LeetCode】Day106-单词规律
2022-07-29 12:55:00 【倒过来是圈圈】
题目
题解
单词和pattern中的字母要一一对应,因此需要两个映射,即建立两个哈希表
循环里的逻辑需要注意一下,一开始 if else有点乱,没有ac
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] strings=s.split("\\s+");
int n=strings.length;
if(pattern.length()!=n)
return false;
Map<String,Character>hashMap1=new HashMap<>();
Map<Character,String>hashMap2=new HashMap<>();
for(int i=0;i<n;i++){
String word=strings[i];
char letter=pattern.charAt(i);
//有但不等
if(hashMap1.containsKey(word)&&!hashMap1.get(word).equals(letter))
return false;
if(hashMap2.containsKey(letter)&&!hashMap2.get(letter).equals(word))
return false;
//没有
hashMap1.put(word,letter);
hashMap2.put(letter,word);
}
return true;
}
}
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)
边栏推荐
猜你喜欢

mysql5.7.35安装配置教程【超级详细安装教程】

传奇人形怪爆率怎么设置?人形怪增加教程

AutoAlignV2:多模态3D目标检测新SOTA!(ECCV2022)

MySQL基础(DDL、DML、DQL)

IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一

阿里云官方 Redis 开发规范!

html+css+php+mysql实现注册+登录+修改密码(附完整代码)

pycharm专业版使用

MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)

IO flow: node flow and process flow summarized in detail.
随机推荐
IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
近期论文总结
Research on the thinking and application methods of the frontier of ESI research
[WeChat applet] WXSS and global, page configuration
What should I do if the webpage is hijacked and redirected?Release net repair method
2022 IDEA (学生邮箱认证)安装使用教程以及基础配置教程
【MySQL】ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘
别再问我如何制作甘特图了!
conda环境创建及复制
Framework common annotation explanation
npm install 报错问题解决合集
IO flow: node flow and process flow summarized in detail.
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
Legendary version adds npc modification, adds npc method and configuration parameter tutorial
nacos cluster construction
Linux下 mysql5.7的彻底卸载
为什么用了大牌工具后报表开发依然头痛
RedisTemplate使用详解
C language game ------ greedy snake ---- for Xiaobai
网页被劫持跳转怎么办?发布网修复方法