当前位置:网站首页>Word pattern for leetcode topic analysis
Word pattern for leetcode topic analysis
2022-06-23 06:17:00 【ruochen】
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
- pattern = "abba", str = "dog cat cat dog" should return true.
- pattern = "abba", str = "dog cat cat fish" should return false.
- pattern = "aaaa", str = "dog cat cat dog" should return false.
- pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Guarantee 1 Yes 1 Mapping
public boolean wordPattern(String pattern, String str) {
if (pattern == null || str == null) {
return false;
}
String[] strs = str.split(" ");
if (pattern.length() != strs.length) {
return false;
}
Map<Character, String> map = new HashMap<Character, String>();
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
if (map.containsKey(c)) {
if (!map.get(c).equals(strs[i])) {
return false;
}
} else {
// Guarantee 1 Yes 1 Mapping
if (map.containsValue(strs[i])) {
return false;
}
map.put(c, strs[i]);
}
}
return true;
}边栏推荐
- [cocos2d-x] erasable layer:erasablelayer
- Ansible uses ordinary users to manage the controlled end
- 【DaVinci Developer专题】-42-如何生成APP SWC的Template和Header文件
- Layer 2技术方案进展情况
- Tencent security 2021 report white paper collection (download attached)
- 基于T5L1的小型PLC设计方案
- Pat class B 1023 minimum decimals
- Wireshark TS | 视频 APP 无法播放问题
- [DaVinci developer topic] -42- how to generate template and header files of APP SWC
- SSM project construction
猜你喜欢

Day_05 传智健康项目-预约管理-预约设置

Adnroid activity screenshot save display to album view display picture animation disappear

Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference

Pyqt5 setting window top left Icon

Radar canvas

mysql读已提交和可重复度区别

Day_13 傳智健康項目-第13章

基于T5L1的小型PLC设计方案

How to specify the output path of pig register Project Log

SSM project construction
随机推荐
Pat class B 1024 scientific notation C language
100-300 cases of single chip microcomputer program (detailed explanation of notes)
JVM原理简介
Machine learning 3-ridge regression, Lasso, variable selection technique
How to add libraries for Arduino ide installation
Day_10 传智健康项目-权限控制、图形报表
Redis 哨兵
Android handler memory leak kotlin memory leak handling
New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!
Wechat tried out the 1065 working system, and was forced to leave work at 18:00; It is said that Apple will no longer develop off screen fingerprint identification; Amd chief independent GPU architect
Infotnews | which Postcard will you receive from the universe?
jvm-02. Guarantee of orderliness
Pyinstaller 打包pyttsx3 出错
[focus on growth and build a dream for the future] - TDP year-end event, three chapters go to the Spring Festival!
Design scheme of Small PLC based on t5l1
SQL statement error caused by the same SQL table name and function name.
最优传输理论下对抗攻击可解释性
JS interview question - anti shake function
去除防火墙和虚拟机对live555启动IP地址的影响
jvm-03. JVM memory model