当前位置:网站首页>【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)
边栏推荐
猜你喜欢
随机推荐
如何成为一名获得 Adobe 国际认证的专业设计师?
The strategy pattern replacement if the else
【MySQL】ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘
mysql5.7.35安装配置教程【超级详细安装教程】
Go - reading (7), CopySheet Excelize API source code (the from and to the int)
浅谈防勒索病毒方案之主机加固
[WeChat applet] WXSS and global, page configuration
传奇版本添加npc修改增加npc方法以及配置参数教程
Mysql stored procedures, rounding
JUC阻塞队列-ArrayBlockingQueue
Sql文件导入数据库-保姆级教程
pycharm专业版使用
DBeaver 安装及配置离线驱动
MySQL基础篇(三)-- 数据类型
理解yolov7网络结构
开关电源-LLC基本原理
Go简单实现协程池
阿里云官方 Redis 开发规范!
来自 Qt 官网的呐喊
传奇人形怪爆率怎么设置?人形怪增加教程



![[Numpy] 创建数组](/img/a8/1c2dc07d08b70a4ec66d8070f218b9.png)





