当前位置:网站首页>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;
}边栏推荐
- 密码学系列之:PKI的证书格式表示X.509
- [open source project] excel export Lua configuration table tool
- Pat class B 1017 C language
- Pyinstaller 打包pyttsx3 出错
- Excel sheet column title for leetcode Title Resolution
- SQL statement error caused by the same SQL table name and function name.
- [cocos2d-x] erasable layer:erasablelayer
- jvm-04. Object's memory layout
- gplearn出现 assignment destination is read-only
- [vivado] xilinxcedstore introduction
猜你喜欢

Activity startup mode and life cycle measurement results

Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library

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

Visual Studio调试技巧

Summary of ant usage (I): using ant to automatically package apk

Pyqt5 设置窗口左上角图标

最优传输理论下对抗攻击可解释性

(1)基础学习——vim编辑器常用快捷操作命令

100-300 cases of single chip microcomputer program (detailed explanation of notes)

Wireshark TS | video app cannot play
随机推荐
jvm-03.jvm内存模型
jvm-04.对象的内存布局
Operating mongodb in node
jvm-05.垃圾回收
jvm-06.垃圾回收器
Remove duplicates from sorted list II of leetcode topic resolution
Tencent security 2021 report white paper collection (download attached)
Tcp/ip explanation (version 2) notes / 3 link layer / 3.3 full duplex, energy saving, automatic negotiation mechanism, 802.1x flow control / 3.3.3 link layer flow control
Pat class B 1017 C language
SSM project construction
The hierarchyviewer tool cannot find the hierarchyviewer location
(1)基础学习——vim编辑器常用快捷操作命令
jvm-06. Garbage collector
Summary of ant usage (I): using ant to automatically package apk
Day_11 传智健康项目-图形报表、POI报表
机器学习3-岭回归,Lasso,变量选择技术
Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library
Addressing and addressing units
【Cocos2d-x】可擦除的Layer:ErasableLayer
Cloud native database is the future