当前位置:网站首页>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;
}边栏推荐
- jvm-02. Guarantee of orderliness
- Remove duplicates from sorted list II of leetcode topic resolution
- Pat class B 1015 C language
- 【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
- Add and multiply two polynomials using linked list
- Find the number of nodes in the widest layer of a binary tree
- Tencent security 2021 report white paper collection (download attached)
- Implementation of linear list linked list structure
- jvm-04.对象的内存布局
- New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!
猜你喜欢

jvm-01.指令重排

Pyqt5 setting window top left Icon

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

Pyqt5 设置窗口左上角图标

gplearn出现 assignment destination is read-only

Efficient office of fintech (I): automatic generation of trust plan specification

Visual Studio调试技巧
![[cocos2d-x] screenshot sharing function](/img/fc/e3d7e5ba164638e2c48bc4a52a7f13.png)
[cocos2d-x] screenshot sharing function

Ant Usage Summary (II): description of related commands

jvm-06.垃圾回收器
随机推荐
Progress of layer 2 technical scheme
使用aggregation API扩展你的kubernetes API
How to specify the output path of pig register Project Log
Design scheme of Small PLC based on t5l1
Vant web app calendar component performance optimization calendar add min date the minimum date page loads slowly
(1) Basic learning - Common shortcut commands of vim editor
Difference between MySQL read committed and repeatability
gplearn出现 assignment destination is read-only
Pyinstaller package exe setting icon is not displayed
Alibaba cloud ack one and ACK cloud native AI suite have been newly released to meet the needs of the end of the computing era
Wireshark TS | 视频 APP 无法播放问题
Remove the influence of firewall and virtual machine on live555 startup IP address
Paper notes: multi label learning lsml
又到半年总结时,IT人只想躺平
[cocos2d-x] custom ring menu
jvm-04.对象的内存布局
Cryptography series: certificate format representation of PKI X.509
mongodb 4. X binding multiple IP startup errors
Android handler memory leak kotlin memory leak handling
Ansible uses ordinary users to manage the controlled end